Same redefinition of only a specific character

You can see this template in the in.h header file :

enum {
  IPPROTO_IP = 0,               /* Dummy protocol for TCP               */
#define IPPROTO_IP              IPPROTO_IP
  IPPROTO_ICMP = 1,             /* Internet Control Message Protocol    */
#define IPPROTO_ICMP            IPPROTO_ICMP

What is the reason for overriding an already defined character? If I understand correctly when the preprocessor encounters IPPROTO_ICMP, it will replace it with IPPROTO_ICMP, so nothing will change.

+4
source share
2 answers

One common useful use of self-promotion is to create a macro that expands on its own. If you write

#define EPERM EPERM

EPERM EPERM. , , . , #ifdef. , enum, #ifdef .

: http://gcc.gnu.org/onlinedocs/gcc-4.6.2/cpp/Self_002dReferential-Macros.html#Self_002dReferential-Macros

+3

, C! - :

enum {
    VAL_0 = 0,
    VAL_1,
};
#if (!defined(VAL_0))
#   error "VAL_0 not defined!"
#endif

! :

enum {
    VAL_0 = 0,
#   define VAL_0    VAL_0
    VAL_1,
#   define VAL_1    VAL_1
};
#if (!defined(VAL_0))
#   error "VAL_0 not defined!"
#endif

EDIT:

Twinkle, , . , :

#if (VAL_1 != 1)
#   error "VAL_1 just defined and have default value 0"
#endif
+2

All Articles