C #error and #pragma

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is #error and #pragma?

#error "message" makes compilation fail with your message, which is useful for unsupported configurations. #pragma passes a compiler-specific hint such as #pragma once or #pragma pack. Unknown pragmas are ignored. C99 also provides the _Pragma("...") operator for use inside macros.

Remember
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error "C99 or newer is required"
#endif

#pragma once
#pragma pack(push, 1)   /* ...struct... */  #pragma pack(pop)

Cheat Sheet

Keep these facts handy while you work with #error and #pragma in C.

TopicDetail
#errorFails compilation with a message
#warningCommon extension, not standard
#pragmaImplementation-defined
_PragmaPragma usable in macros (C99)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Advanced Concepts.

Example 1 — Require C99 and pack a struct

pack(1) removes padding in GCC, Clang, and MSVC.

C
#include <stdio.h>

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error "C99 or newer is required"
#endif

#pragma pack(push, 1)
struct Packed { char c; int i; };
#pragma pack(pop)

int main(void) {
    printf("built as C99 or newer\n");
    printf("sizeof Packed = %zu\n", sizeof(struct Packed));
    return 0;
}

How It Works

  1. If the compiler is older than C99, #error stops the build with a clear message.
  2. pack(push, 1) aligns members on 1 byte, so char + int is 5 bytes.
  3. pack(pop) restores the previous alignment.

Edge Cases & Pitfalls

Check these before relying on #error and #pragma in real programs.

port

Pragmas are not portable

Another compiler may ignore or reject a pragma. Wrap with #ifdef when needed.

pack

Unaligned access

Packed structs may cause slow or invalid unaligned access on some CPUs.

Key Takeaways

  • #error: fail early with a reason.
  • #pragma: compiler-specific.
  • Test portability.

One line: #error "message" makes compilation fail with your message, which is useful for unsupported configurations.

Frequently Asked Questions

It stops compilation and prints the message you supply. It is usually placed inside an #if to reject unsupported configurations.
It tells the compiler to include the header only once per translation unit, similar to an include guard.
Continue with "C Advanced Concepts" next. Go beyond the basics: command-line arguments, modular code, qualifiers, error handling, assertions, undefined behavior, portability, UTF-8, and C standards. The C Preprocessor hub lists every topic in order.

Next: Advanced Concepts

Go beyond the basics: command-line arguments, modular code, qualifiers, error handling, assertions, undefined behavior, portability, UTF-8, and C standards.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful