Static, define, and const in C

前端 未结 9 2048
半阙折子戏
半阙折子戏 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:34

    static for an object declared outside a function merely makes the object local to the translation unit (i.e. it can't be accessed from other .c files). It doesn't make it constant. That it was const is for. They are orthogonal so you can have one or the other or both.

    e.g.

    static const double m = 5;
    

    The #define declares a macro which (in this case) can be used as a constant value. There's no object, so const doesn't apply as there's not object to be changed. As a consequence, you also can't take the address of a macro.

提交回复
热议问题