Static, define, and const in C

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

    The main difference is that with #define you leave the type system. The preprocessor has no notion of type safety, scope etc. So e.g. if you later try to write a loop like

    for (int m = 0; m < size; m++) { ... }

    you are up to a nasty surprise...

    Also if you use #defines, you will only see the value of 30000 when debugging your code, not the name m. Which does not seem to make a big difference in this case, but when using meaningful constant and variable names, it does indeed.

提交回复
热议问题