Does GNU cpp consider the odd C99 standard for macros null arguments?

Why GNU cpp accepts the following code, even if it starts with the -std = c99 -pedantic flags:

#define z()
#define w(x)
z()
w()
w(1)

The C99 standard requires that the number of arguments in a macro call corresponding to functions correspond to the number of parameters in the macro definition (and is pleased with the idea that the argument may consist of an empty sequence of preprocessing tokens, so the first two calls provide a single empty argument, however), but this is not may be true for all three calls.

Indeed, z should only be called with zero arguments, which is syntactically impossible?

+4
source share
4 answers

Several experiments show that the first line is winterpreted as one empty argument:

#define w(x) #x
w()
w(1)

when preprocessed gives:

""
"1"

Better:

#define w(x, y) #x <-> #y
w(,)
w(1,)
w(, 2)
w(1, 2)

gives:

"" <-> ""
"1" <-> ""
"" <-> "2"
"1" <-> "2"

Acuity ...

I don’t know what the standard says about this. Gotta ask a lawyer ...

+2
source

Using macros differs from calling a function in one important aspect: macro arguments can be empty. It's really:

#define x(a,b)  X a X b X
x(,)  //-> X X X
x(1,) //-> X 1 X X
x(,2) //-> X X 2 X

Given that null arguments are allowed, this is the use of a macro:

#define x(a) X a X
x()  //-> X X

should be interpreted as an empty argument. Although this one:

#define x() XX
x()  //-> XX

- macro without arguments.

+1
source

, , :

C 2011. 6.10.3 . 10:

:

# define identifier lparen identifier-list_opt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line

- , .

. :

#define p() int
#define t(a) a
#define glue(a, b) a ## b

p()       // Produces: int
t(x)      // Produces: x
glue(1,2) // Produces: 12
0

. - x(), . GCC , :

#define x()  replacement
#define y(a) repl##a##cement

x() y()

replacement replcement

C99 6.10, , , , , , , , GCC - .; -)

There is some discussion of these rules in relatively informal terms at the bottom of https://gcc.gnu.org/onlinedocs/gcc-4.8.1/cpp/Macro-Arguments.html . Please note in particular that this

#define z(a,b) stuff with a and b

This is a write error z(), but z(,)valid.

0
source

Source: https://habr.com/ru/post/1528797/


All Articles