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 once

// header declarations here
#pragma warning (MSVC) — suppress or enable specific warnings:
#pragma warning(disable : 4996)   // suppress deprecated function warning
#pragma comment (MSVC) — link a library:
#pragma comment(lib, "ws2_32.lib")
#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:
#include <stdio.h>

#ifndef MAX_SIZE
    #error "MAX_SIZE must be defined before including this file!"
#endif

int arr[MAX_SIZE];
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:
#line 100 "myfile.c"
// Compiler will now report the next line as line 100 in "myfile.c"
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:
MacroTypeDescription
__FILE__char *Current source file name
__LINE__intCurrent line number
__DATE__char *Compilation date
__TIME__char *Compilation time
__func__char *Current function name (C99+)
__STDC__int1 if the compiler conforms to the C standard
Example:
#include <stdio.h>

void myFunction() {
    printf("Function : %s\n", __func__);
    printf("File     : %s\n", __FILE__);
    printf("Line     : %d\n", __LINE__);
    printf("Compiled : %s at %s\n", __DATE__, __TIME__);
}

int main() {
    myFunction();
    return 0;
}
Output (example):
Function : myFunction
File     : main.c
Line     : 5
Compiled : Jun 13 2026 at 14:30:00
These are very useful for debugging and logging.

#undef

Remove a previously defined macro:
#define LIMIT 100
// ... use LIMIT ...
#undef LIMIT
// LIMIT is no longer defined from here