开发者

where and when to use const in C

Where all can const be used in C?

Also when s开发者_JS百科hould i use it?


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜