Difference between Enum and Define Statements

后端 未结 18 2574
终归单人心
终归单人心 2020-11-30 00:27

What\'s the difference between using a define statement and an enum statement in C/C++ (and is there any difference when using them with either C or C++)?

For exampl

18条回答
  •  不知归路
    2020-11-30 01:08

    enum defines a syntactical element.

    #define is a pre-preprocessor directive, executed before the compiler sees the code, and therefore is not a language element of C itself.

    Generally enums are preferred as they are type-safe and more easily discoverable. Defines are harder to locate and can have complex behavior, for example one piece of code can redefine a #define made by another. This can be hard to track down.

提交回复
热议问题