Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. #pragma / #error / #warning

#pragma / #error / #warning

These are supplementary preprocessor directives. #pragma controls compiler-specific behavior, #error forcibly stops compilation and displays a message, and #warning outputs a warning message.

Syntax

// Controls compiler-specific behavior (varies by compiler).
#pragma directive

// A concise way to prevent double inclusion (supported by most compilers).
#pragma once

// Stops compilation immediately and displays an error message.
#error error message

// Displays a warning message (supported by GCC, Clang, etc.).
#warning warning message

// Controls structure padding (use with caution for portability).
#pragma pack(n)   // Aligns members on n-byte boundaries.
#pragma pack()    // Restores the default alignment.

Common Directives

DirectiveDescription
#pragma onceTells the compiler to include the header file only once. Supported by most compilers, but is not part of the C standard.
#pragma pack(n)Sets the member alignment of a structure to n-byte boundaries. Used to match structures with network protocols or external binary formats.
#pragma warningEnables or disables specific warnings (supported by Visual C++, etc.).
#errorStops compilation at the preprocessor stage and displays the specified message as an error.
#warningDisplays a warning message at the preprocessor stage. Compilation continues. Supported by GCC and Clang.

Sample Code

// Example: myheader.h
// Use #pragma once for a concise header guard.
#pragma once

#define MY_HEADER_DEFINED

// Check the C standard version and control compilation accordingly.
#if __STDC_VERSION__ < 199901L
    #error "This code requires C99 or later."
#endif

// Warn about a feature planned for removal (GCC / Clang).
// #warning "This function is deprecated. Use new_func() instead."
// Example: main.c
#include <stdio.h>

// Lay out the structure in memory without padding (example: network packet).
#pragma pack(1)
typedef struct {
    unsigned char  type;    // 1 byte
    unsigned short length;  // 2 bytes (no padding)
    unsigned int   data;    // 4 bytes
} PacketHeader;
#pragma pack() // Restore default alignment.

// Compare with a padded structure.
typedef struct {
    unsigned char  type;
    unsigned short length;
    unsigned int   data;
} PacketHeaderPadded;

int main(void) {
    printf("Size with pack(1): %zu bytes\n", sizeof(PacketHeader));
    // Prints "7" (no padding).

    printf("Normal size: %zu bytes\n", sizeof(PacketHeaderPadded));
    // May be 8 or more depending on the environment, due to alignment.

    return 0;
}

Notes

The C standard specifies that #pragma directives unknown to the compiler must be ignored. As a result, the directives supported by each compiler vary. #pragma pack changes the memory layout of structures, so misuse can cause alignment violations, leading to performance degradation or crashes. Limit its use to specific purposes such as network packets.

#error is useful for enforcing required preconditions — such as the C standard version, specific macro definitions, or the target architecture. Combined with conditional compilation, it lets you catch incorrect build environments early.

For more on conditional compilation, see #ifdef / #ifndef / #endif. For macro definitions, see #include / #define (constants).

If you find any errors or copyright issues, please .