Home > Mobile >  A different number of scans to expand the C preprocessor macro
A different number of scans to expand the C preprocessor macro

Time:12-30

such a question below is a simple macro, however, the compilation behavior of msvc and gnu / clang is different, so to expand macro A in msvc, you need 1 scan more than gnu / clang, why is this happening?

#include <stdio.h>

#define EMPTY()
#define DEFER_(X) X EMPTY()
#define DEFER1(...) __VA_ARGS__ DEFER_(EMPTY)()
#define DEFER(...) __VA_ARGS__ DEFER_(EMPTY)()
#define TO_STR(X) TO_STR_(X)
#define TO_STR_(X) #X
#define A() 123
#define EXPAND(...) __VA_ARGS__
#define EXPAND1(...) EXPAND(__VA_ARGS__)
#define EXPAND2(...) EXPAND1(__VA_ARGS__)
int main(void)
{
 printf(TO_STR(EXPAND1(DEFER(A)()))"\n");
}

The behavior for all compilers presented on godbolt is the same as gnu except msvc.

CodePudding user response:

By default, MSVC does not use standard C or C rules for preprocessing. Use the switch /Zc:preprocessor to request standard-compliant preprocessing. You can also use /std:c17 to request conformance to the C standard generally, not just in preprocessing, although conformance may not be complete.

  • Related