Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using #define in C with no value

Tags:

c

macros

If a #define is used with no value, like

#define COMMAND_SPI()

does it take value 0 by default?

like image 450
user2990780 Avatar asked Jun 12 '14 22:06

user2990780


1 Answers

No, it evaluates to nothing. Literally the symbol gets replaced with nothing.

However, once you have #define FOO, the preprocessor conditional #ifdef FOO will now be true.

Note also that in gcc and possibly other compilers, if you define a macro with -DFOO on the command line, that evaluates to 1 by default.


Since the OP updated his question to reference function-like macros, let's consider a small example.

#define FOO
#define BAR()


FOO
BAR
BAR()

This is not a valid C program, but the preprocessor does not care. If I compile this with gcc -E Input.c, I get a blank, followed by BAR followed by another blank. This is because the first and third expressions evaluate to nothingness, and the middle expression is not expanded because there are no () after it.

like image 73
merlin2011 Avatar answered Oct 17 '22 01:10

merlin2011