Difference between Enum and Define Statements

后端 未结 18 2475
终归单人心
终归单人心 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 00:52

    There is little difference. The C Standard says that enumerations have integral type and that enumeration constants are of type int, so both may be freely intermixed with other integral types, without errors. (If, on the other hand, such intermixing were disallowed without explicit casts, judicious use of enumerations could catch certain programming errors.)

    Some advantages of enumerations are that the numeric values are automatically assigned, that a debugger may be able to display the symbolic values when enumeration variables are examined, and that they obey block scope. (A compiler may also generate nonfatal warnings when enumerations are indiscriminately mixed, since doing so can still be considered bad style even though it is not strictly illegal.) A disadvantage is that the programmer has little control over those nonfatal warnings; some programmers also resent not having control over the sizes of enumeration variables.

提交回复
热议问题