Skip to main content

Other Preprocessor Commands

Beyond #define, #include, and #ifdef, the C preprocessor provides several other useful directives.

#pragma

#pragma provides implementation-specific (compiler-specific) instructions. The most widely-used are: #pragma once — modern include guard (prevents a header from being included multiple times):
#pragma warning (MSVC) — suppress or enable specific warnings:
#pragma comment (MSVC) — link a library:
#pragma directives are not portable — they vary between compilers. Code using them may not compile on a different compiler.

#error

Forces a compile-time error with a custom message. Useful for catching configuration mistakes:
If MAX_SIZE is not defined, compilation stops immediately with the specified message.

#line

Changes the line number (and optionally the filename) reported by the compiler in error messages:
Mainly used by code-generation tools to keep error messages pointing to the original source.

Predefined Macros

The C standard defines several macros that are automatically available in every program: Example:
Output (example):
These are very useful for debugging and logging.

#undef

Remove a previously defined macro: