122

I would like to be able to do something like

#print "C Preprocessor got here!"

for debugging purposes. What's the best / most portable way to do this?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Andrew Wagner
  • 20,197
  • 19
  • 75
  • 95

7 Answers7

144

The warning directive is probably the closest you'll get, but it's not entirely platform-independent:

#warning "C Preprocessor got here!"

AFAIK this works on most compilers except MSVC, on which you'll have to use a pragma directive:

#pragma message ( "C Preprocessor got here!" )
You
  • 21,633
  • 3
  • 49
  • 64
  • 5
    Which begs the question, can you put a directive based on a compilation flag to swap "pragma message" and "warning" somehow? For example, something like: `#ifdef _LINUX #define #preprocmsg "#warning" else #define #preprocmsg "#pragma message"`... I'll have to try that but instinct tells me the answer is no. – Bryan Sep 30 '10 at 00:41
  • 12
    @Bryan: Yes. `#define WARNING(msg) _Pragma("message " #msg)` – Matt Joiner Mar 02 '11 at 07:31
  • 1
    #pragma message () is not supported by older versions of gcc (such as gcc 4.1.2, the default version on RHEL5). I have yet to find an appropriate equivalent for these older versions - #warning is not going to be great, as warnings are treated as errors for us generally, and we'd really like the message to be informational, rather than stop the compilation. – Danny S Mar 23 '15 at 04:10
  • 8
    Issuing a warning is very inconvenient when your project compiles with -Wall by default. #pragma message doesn't have that problem. – Renan Gemignani Feb 21 '17 at 14:49
  • is there someway to substitute in macro values, is that possible? Maybe something like `#warning "DEBUG="DEBUG` – jxramos Jul 08 '21 at 23:11
61

The following are supported by MSVC, and GCC.

#pragma message("stuff")
#pragma message "stuff"

Clang has begun adding support recently, see here for more.

Community
  • 1
  • 1
Matt Joiner
  • 106,562
  • 103
  • 351
  • 513
  • 3
    Just for the record, Solaris Studio 12.3 (Sun C 5.12) does not support this pragma. – maxschlepzig May 28 '13 at 13:06
  • Works with Arduino 1.8 using Visual Studio vMicro. Thanks! – save_jeff Mar 29 '20 at 07:35
  • Supported by clang at least since 3.0 and by gcc since 5.1 (and in a slightly different form since 4.4.7). Even if clang says "1 warning generated", it doesn't fail build when built with `-Werror`: https://gcc.godbolt.org/z/xoK6b8 – ead Nov 21 '20 at 23:24
12

You might want to try: #pragma message("Hello World!")

Ruel
  • 14,929
  • 7
  • 36
  • 49
11

Most C compilers will recognize a #warning directive, so

 #warning "Got here"

There's also the standard '#error' directive,

 #error "Got here"

While all compilers support that, it'll also stop the compilation/preprocessing.

nos
  • 215,098
  • 54
  • 400
  • 488
8
#pragma message("foo")

works great. Also wouldn't stop compilation even if you use -Werror

Martin
  • 3,332
  • 3
  • 23
  • 29
  • Is there a way to make this work with numbers or other values? It says it expects a string, but I would like to print out what a numeric `#define` is calculated to be. – Aaron Franke Jan 22 '21 at 10:28
  • @AaronFranke During the preprocessor step, all defines should be ASCII. – Tzalumen Mar 22 '22 at 21:19
0

Another solution is to use comments plus a shell script to process them. This takes some discipline (or a shell script which catches typos).

For example, I add comments formatted //TODO and then a shell script which collects all of them into a report.

For more complex use cases, you can try to write your own simple preprocessor. For example, you could edit your sources as *.c2 files. The simple preprocessor would read the source, look for //TODO, and write printf("TODO ...") into the output *.c file.

Aaron Digulla
  • 310,263
  • 103
  • 579
  • 794
-23

You can't. Preprocessors are processed before the C code. There are no preprocessor directives to print to the screen, because preprocessor code isn't executed, it is used to generate the C code which will be compiled into executable code.

Anything wrong with:

#ifdef ...
printf("Hello");
#endif

Because this is all you can do as far as preprocessors go.

Alexander Rafferty
  • 6,034
  • 2
  • 31
  • 55