What are the major advantages of const versus #define for global constants?

前端 未结 7 1123
北恋
北恋 2021-01-14 10:28

In embedded programming, for example, #define GLOBAL_CONSTANT 42 is preferred to const int GLOBAL_CONSTANT = 42; for the following reasons:

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 11:09

    I think the main advantage is that you can change the constant without having to recompile everything that uses it.

    Since a macro change will effectively modify the contents of the file that use the macro, recompilation is necessary.

提交回复
热议问题