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

后端 未结 17 1455
一生所求
一生所求 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:27

    I am not sure if I am right but in my opinion calling #defined value is much faster than calling any other normally declared variable (or const value). It's because when program is running and it needs to use some normally declared variable it needs to jump to exact place in memory to get that variable.

    In opposite when it use #defined value, the program don't need to jump to any allocated memory, it just takes the value. If #define myValue 7 and the program calling myValue, it behaves exactly the same as when it just calls 7.

提交回复
热议问题