Many people are familiar with the classic #ifdef and #define preprocessor commands in C. However, there are many more that are actually surprisingly useful:


This is useful for extracting variable names, enum names, and compound expressions (think how assert is able to print the expression). This is done with the # prefix as follows:

#define stringof(x) #x</p>
char *hello = 0;
printf("%s\n", stringof(hello));

output: "hello"

Cause compilation to stop upon reaching a line of code.

This is useful when you have incompatible #define statements, such as using DirectX on a Linux build. This is logically performed with the #error statement.

Cause compilation to report a warning upon reaching a line of code.

This is very similar to #error, and is instead logically labeled #warning.

Replace header define guards.

I don’t know why this isn’t more common, every modern C compiler seems to support the statement #pragma once, which tells the compiler only to include the file once.

Produce a custom bookmark in your code.

The #pragma mark command will produce a bookmark in your code that you can jump to. For instance, you could have the code:

struct person
	#pragma mark Public Functions
		void work();
	#pragma mark Private Functions
		void sleep();

This should create two bookmarks in your text editor (often seen with the class/function hierarchy viewer), one for public functions, and another for private functions.

Variable number of macro parameters.

Starting in C99, the preprocessor supports variadic macro parameters. This means you can use variable number of macro parameters like with varargs. Functions with an ellipsis are expanded to __VA_ARGS__. Wikipedia provides a nice example duplicated for convenience and posterity:

void realdprintf (char const *file, int line, char const *fmt, ...);
#define dprintf(...) realdprintf(__FILE__, __LINE__, __VA_ARGS__)

Define structure packing.

There are several structure packing pragmas that describe alignment to the compiler. This can also be done with __attribute__. More information here and here.

Token concatenation.

This one is pretty nifty. Say you have a function call that depends upon the name of the variable. You can use token concatenation to generate a token that specifies the function of interest:

#include <stdio.h>;
#define function_call(base, name) base ##_## name()
#define functioncall(base, name) base ## name()
void function_hello()
	printf("Hello, ");
void fworld()
int main()
	function_call(function, hello);
	functioncall(f, world);
	return 0;

File name and line number.

The macros __FILE__ and __LINE__ produce file name (as char*) and line number (as int), respectively.

Some of this information and more can be found in the GCC online documentation for macros:, pragmas, and Microsoft’s MSVC preprocessor documentation Be careful of compiler specific preprocessor tricks like push_macro, the ones listed here seem to work fine with both GCC and MSVC.

If you are only using GCC, there is a nice overview of some neat GCC C Extensions here: