Static, define, and const in C

前端 未结 9 2044
半阙折子戏
半阙折子戏 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条回答
  •  萌比男神i
    2020-12-13 05:44

    When you write const double m=3000; you are telling the compiler to create a symbol m in the object file that can be accessed from other files. The compiler may inline the value of m in the file where it is defined, but the symbol still has to be allocated for the purposes of separate compilation.

    When you write #define m 3000 you are just using a syntactic convenience for writing the same constant in several places in the source file.

提交回复
热议问题