user657247
user657247

Reputation: 91

where and when to use const in C

Where all can const be used in C?

Also when should i use it?

Upvotes: 2

Views: 5071

Answers (2)

hotpaw2
hotpaw2

Reputation: 70673

The const keyword in C has nothing to do with constants. (For constants in C, use defines, or perhaps defines that include casts to the appropriate data type for type checking).

The const keyword, when used correctly, is usually a hint to the compiler that some variable won't get changed inside some scope, allowing the compiler to relax restrictions on certain types of optimizations and code scheduling which could produce hazards or aliasing errors if that variable actually was somehow to be modified. Some compilers may produce an error message if they detect this.

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92854

In C const qualified variables don't really define constants.

You should always(in most cases) use #define over const

Upvotes: 2

Related Questions