Error: macro names must be identifiers using #ifdef 0

120,343

Solution 1

The #ifdef directive is used to check if a preprocessor symbol is defined. The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

The correct form for using the pre-processor to block out code is:

#if 0
: : :
#endif

You can also use:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

but you need to be confident that the symbols will not be inadvertently set by code other than your own. In other words, don't use something like NOTUSED or DONOTCOMPILE which others may also use. To be safe, the #if option should be preferred.

Solution 2

Use the following to evaluate an expression (constant 0 evaluates to false).

#if 0
 ...
#endif

Solution 3

This error can also occur if you are not following the marco rules

Like

#define 1K 1024 // Macro rules must be identifiers error occurs

Reason: Macro Should begin with a letter, not a number

Change to

#define ONE_KILOBYTE 1024 // This resolves 

Solution 4

#ifdef 0
...
#endif

#ifdef expect a macro rather than expression when using constant or expression

#if 0
...
#endif

or

#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

if #ifdef is used the it reports this error

#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

Where #ifdef expect a macro rather than macro expresssion

Solution 5

Note that you can also hit this error if you accidentally type:

#define <stdio.h>

...instead of...

#include <stdio.>
Share:
120,343

Related videos on Youtube

Eduardo
Author by

Eduardo

Updated on December 27, 2020

Comments

  • Eduardo
    Eduardo over 3 years

    I have the source code of an application written in C++ and I just want to comment something using:

    #ifdef 0
    ...
    #endif
    

    And I get this error

    error: macro names must be identifiers

    Why is this happening?

    • Jonathan Leffler
      Jonathan Leffler over 15 years
      I think you misremembered; #ifdef 0 is an error in C as well as C++.
    • Eduardo
      Eduardo over 15 years
      You are right I misrembered, It does not work in C, I am not going to delete the question because maybe someone in the future makes the same mistake.
    • K_TGTK
      K_TGTK about 10 years
      @Eduardo Thanks for not deleting the question.
  • Jonathan Leffler
    Jonathan Leffler over 15 years
    I have some code that I compile occasionally that is compiled with -DNEVER_USED. I haven't investigated why - I hate to guess.
  • Jonathan Leffler
    Jonathan Leffler over 15 years
    @Pax: actually, it is very unlikely that the C compiler allows the notation - it would be erroneous if it did. See also my comment to the question. I suggest removing your sentence "It's likely that...".
  • paxdiablo
    paxdiablo over 15 years
    Not all C compilers are ANSI-compliant. I've used plenty of embedded compilers that allow all sorts of trickiness like that. But I'll fix it.