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

前端 未结 7 1128
北恋
北恋 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 10:59

    Another reason that hasn't been mentioned yet is that const variables allow the compiler to perform explicit type-checking, but macros do not. Using const can help prevent subtle data-dependent errors that are often difficult to debug.

提交回复
热议问题