Static, define, and const in C

前端 未结 9 2047
半阙折子戏
半阙折子戏 2020-12-13 04:52

I\'ve read that static variables are used inside function when one doesn\'t want the variable value to change/initialize each time the function is called. But what about def

9条回答
  •  天命终不由人
    2020-12-13 05:26

    If the value of m has to stay the same forever, then of course you can either use

    static const double m = 30000; 
    

    or

    #define m 30000
    

    Just note that in C const objects have external linkage by default, so to get the equivalent const declaration you have to use static const, not just const.

    Also note that in C language const objects are not constants, but rather "constant variables". If you need a true constant (i.e. an entity that forms constant expressions), you have to use either #define or enum constant.

    The latter is normally an issue with integral constants only. In your case of a double the approach with [static] const might work best.

提交回复
热议问题