“static const” vs “#define” vs “enum”

后端 未结 17 1481
一生所求
一生所求 2020-11-21 05:45

Which one is better to use among the below statements in C?

static const int var = 5;

or

#define var 5

o

17条回答
  •  别跟我提以往
    2020-11-21 06:24

    Don't think there's an answer for "which is always best" but, as Matthieu said

    static const

    is type safe. My biggest pet peeve with #define, though, is when debugging in Visual Studio you cannot watch the variable. It gives an error that the symbol cannot be found.

提交回复
热议问题