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

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

    The definition

    const int const_value = 5;
    

    does not always define a constant value. Some compilers (for example tcc 0.9.26) just allocate memory identified with the name "const_value". Using the identifier "const_value" you can not modify this memory. But you still could modify the memory using another identifier:

    const int const_value = 5;
    int *mutable_value = (int*) &const_value;
    *mutable_value = 3;
    printf("%i", const_value); // The output may be 5 or 3, depending on the compiler.
    

    This means the definition

    #define CONST_VALUE 5
    

    is the only way to define a constant value which can not be modified by any means.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:29

    The difference between static const and #define is that the former uses the memory and the later does not use the memory for storage. Secondly, you cannot pass the address of an #define whereas you can pass the address of a static const. Actually it is depending on what circumstance we are under, we need to select one among these two. Both are at their best under different circumstances. Please don't assume that one is better than the other... :-)

    If that would have been the case, Dennis Ritchie would have kept the best one alone... hahaha... :-)

    0 讨论(0)
  • 2020-11-21 06:31

    I wrote quick test program to demonstrate one difference:

    #include <stdio.h>
    
    enum {ENUM_DEFINED=16};
    enum {ENUM_DEFINED=32};
    
    #define DEFINED_DEFINED 16
    #define DEFINED_DEFINED 32
    
    int main(int argc, char *argv[]) {
    
       printf("%d, %d\n", DEFINED_DEFINED, ENUM_DEFINED);
    
       return(0);
    }
    

    This compiles with these errors and warnings:

    main.c:6:7: error: redefinition of enumerator 'ENUM_DEFINED'
    enum {ENUM_DEFINED=32};
          ^
    main.c:5:7: note: previous definition is here
    enum {ENUM_DEFINED=16};
          ^
    main.c:9:9: warning: 'DEFINED_DEFINED' macro redefined [-Wmacro-redefined]
    #define DEFINED_DEFINED 32
            ^
    main.c:8:9: note: previous definition is here
    #define DEFINED_DEFINED 16
            ^
    

    Note that enum gives an error when define gives a warning.

    0 讨论(0)
提交回复
热议问题