#pragma Directive in C

Last Updated : 31 Jul, 2026

The #pragma directive is a special preprocessor directive used to provide compiler-specific instructions. It allows you to enable or disable certain compiler features or control compiler behavior.

  • Used to control compiler settings or provide additional instructions during compilation.
  • Compiler-specific, so its behavior may vary between different C compilers.

Syntax

#pragma directive_name

Commonly Used #pragma Directives in C

Some of the commonly used #pragma directives are discussed below:

1. #pragma startup and #pragma exit

These directives help us to specify the functions that are needed to run before the program starts ( before the control passes to the main()) and just before the program exits (just before the control returns from the main()).

C
#include <stdio.h>

void func1();
void func2();

#pragma startup func1
#pragma exit func2

void func1() { printf("Inside func1()\n"); }

void func2() { printf("Inside func2()\n"); }

int main()
{
    printf("Inside main()\n");

    return 0;
}

Output
Inside main()

This happens because GCC does not support #pragma startup or exit. However, you can use the below code for a similar output on GCC compilers.

C
#include <stdio.h>

void func1();
void func2();

void __attribute__((constructor)) func1();
void __attribute__((destructor)) func2();

void func1() { printf("Inside func1()\n"); }

void func2() { printf("Inside func2()\n"); }

int main()
{
    printf("Inside main()\n");

    return 0;
}

Output
Inside func1()
Inside main()
Inside func2()

Syntax

#pragma startup function_name

#pragma exit function_name

2. #pragma warn Directive

#pragma warning is used to suppress compiler warning messages during compilation, allowing developers to focus on fixing errors first. Warnings can be re-enabled later by modifying the pragma directive.

C
#include<stdio.h> 
  
#pragma warn -rvl /* return value */ 
#pragma warn -par /* parameter never used */ 
#pragma warn -rch /*unreachable code */ 
        
int show(int x) 
{    
    // parameter x is never used in 
    // the function 
      
    printf("GEEKSFORGEEKS"); 
      
    // function does not have a 
    // return statement 
} 
            
int main() 
{ 
    show(10); 
      
    return 0; 
} 

Output
GEEKSFORGEEKS

Syntax

#pragma warn +xxx (To show the warning)

#pragma warn -xxx (To hide the warning)

#pragma warn .xxx (To toggle between hide and show)

We can hide the warnings as shown below:

  • #pragma warn -rvl: This directive hides those warning which are raised when a function which is supposed to return a value does not return a value.
  • #pragma warn -par: This directive hides those warning which are raised when a function does not uses the parameters passed to it.
  • #pragma warn -rch: This directive hides those warning which are raised when a code is unreachable. For example: any code written after the return statement in a function is unreachable.

3. #pragma GCC poison

This directive is supported by the GCC compiler and is used to remove an identifier completely from the program. If we want to block an identifier then we can use the #pragma GCC poison directive.

C
#include <stdio.h>

#pragma GCC poison printf

int main()
{
    int a = 10;

    if (a == 10) {
        printf("GEEKSFORGEEKS");
    }
    else
        printf("bye");

    return 0;
}


Output

prog.c: In function 'main':
prog.c:14:9: error: attempt to use poisoned "printf"
printf("GEEKSFORGEEKS");
^
prog.c:17:9: error: attempt to use poisoned "printf"
printf("bye");
^

The program will give the above error.

Syntax

#pragma GCC poison identifier

4. #pragma GCC dependency

The #pragma GCC dependency allows you to check the relative dates of the current file and another file. If the other file is more recent than the current file, a warning is issued. This is useful if the current file is derived from the other file, and should be regenerated.

C
// C program to demonstrate use of

#include <stdio.h>

// Using #pragma GCC dependency to check if "parse.y" file
// has been modified
#pragma GCC dependency "solution.c"

int main()
{
    printf("Hello, Geek!");
    return 0;
}

Syntax

#pragma GCC dependency "main.c"

5. #pragma GCC system_header

#pragma GCC system_header tells the GCC compiler to treat the current file as a system header, suppressing warnings generated from the remaining code in that file. It takes no arguments and is useful when you want to ignore warnings from specific header files.

C
// external_library.h
#ifndef EXTERNAL_LIBRARY_H
#define EXTERNAL_LIBRARY_H

#pragma GCC system_header

void externalFunction();

#endif

The above code is for external library that we want to include in our main code. So to inform the compiler that external_library.h is a system header, and we don't want to see warnings from it the #pragma GCC system_header is used.

C
#include <stdio.h>
#include "external_library.h"

int main() {
    externalFunction();  // Using a function from the external library

    printf("Hello, Geek!\n");
    return 0;
}


Output

Hello, Geek!

Explanation: Now inour main program,"external_library.h" header is included and the function declared in it is used. Hello,Geek ! is printed without any warnings.

Syntax

#pragma GCC system_header

6. #pragma once

#pragma once ensures that a header file is included only once during compilation, even if it is included multiple times. It works similarly to include guards and prevents multiple inclusion of the same header file.

C
// my_header.h
#pragma once

void myFunction();

Consider the above my_header.h header file in this header file, #pragma once is used to guard against multiple inclusions.

C
// C program to demonstrate the use of pragma once
#include <stdio.h>
#include "my_header.h"

int main() {
    myFunction();  // Using a function from my_header.h

    printf("Hello, World!\n");
    return 0;
}


Output

Hello, World!

Syntax

#pragma once

Comment