Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C enum using macros

Tags:

c

In glibc source code, I found that some enumeration definitions include macro definitions. For example:

// file: glibc/stdlib/fmtmsg.h
enum
{
  MM_HARD = 0x001,  /* Source of the condition is hardware.  */
#define MM_HARD MM_HARD
  MM_SOFT = 0x002,  /* Source of the condition is software.  */
#define MM_SOFT MM_SOFT
...
};

and

// file: glibc/bits/confname.h
enum
  {
    _PC_LINK_MAX,
#define _PC_LINK_MAX            _PC_LINK_MAX
    _PC_MAX_CANON,
#define _PC_MAX_CANON           _PC_MAX_CANON
    _PC_MAX_INPUT,
#define _PC_MAX_INPUT           _PC_MAX_INPUT
...
}

Since the syntax of the text macro replacement is

#define identifier replacement-list

But in the above example, identifier is the same as replacement-list. what's the point?

EDITED: I tried to search _PC_LINK_MAX with grep -r, here is the result:

bits/confname.h:    _PC_LINK_MAX,
bits/confname.h:#define _PC_LINK_MAX                    _PC_LINK_MAX
ChangeLog.old/ChangeLog.9:      * sysdeps/unix/sysv/linux/alpha/fpathconf.c: Handle _PC_LINK_MAX here.
conform/data/unistd.h-data:constant _PC_LINK_MAX
manual/conf.texi:@item _PC_LINK_MAX
posix/annexc.c:  "_PC_ASYNC_IO", "_PC_CHOWN_RESTRICTED", "_PC_LINK_MAX", "_PC_MAX_CANON",
posix/fpathconf.c:    case _PC_LINK_MAX:
posix/getconf.c:    { "LINK_MAX", _PC_LINK_MAX, PATHCONF },
posix/getconf.c:    { "_POSIX_LINK_MAX", _PC_LINK_MAX, PATHCONF },
sysdeps/posix/fpathconf.c:    case _PC_LINK_MAX:
sysdeps/posix/pathconf.c:    case _PC_LINK_MAX:
sysdeps/unix/sysv/linux/fpathconf.c:    case _PC_LINK_MAX:
sysdeps/unix/sysv/linux/pathconf.c:    case _PC_LINK_MAX:
like image 695
Revc_Ra Avatar asked Feb 23 '21 06:02

Revc_Ra


People also ask

How to turn a macro into a proper enum in C?

The library is working using X macro definition for the enum like that: The first step for the library is to build the enum. So we want to build the macro that turn the macro definition of the enum into a proper C enum. This macro will be called by SMARTENUM_DECLARE.

How to declare an enum in C language?

The keyword “enum” is used to declare an enumeration. Here is the syntax of enum in C language, enum enum_name{const1, const2, ..... }; The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows.

What is enumeration in C++?

Last Updated : 21 Dec, 2018. Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0}; The keyword ‘enum’ is used to declare new enumeration types in C and C++.

How to define the variables of enum type in Python?

The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows. In the above program, two enums are declared as week and day outside the main () function. In the main () function, the values of enum elements are printed.


1 Answers

It's a very common workaround. It allows programmer to use macro directives (especially #if) to generate the code if type or enum was declared.

In other standard header files you can see

#ifndef _UINT32_T_DECLARED
typedef __uint32_t uint32_t ;
#define _UINT32_T_DECLARED
#endif

Example usage:

#if defined(_UINT32_T_DECLARED)
typedef my32 uint32_t;
#else
typedef my32 unsigned long;
#endif

#ifdef MM_SOFT
myfunc(MM_SOFT);
#else
#error Required enum missing.
#endif
like image 172
0___________ Avatar answered Oct 26 '22 19:10

0___________