- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
#pragma Directive in C
What is #pragma Directive in C?
The preprocessor directive #pragma is used to provide additional information to the compiler in C/C++ language. This is used by the compiler to provide some special features.
Note that pragmas are compiler dependent. Not all the pragma directives are supported by all the compilers.
Syntax
Here is the syntax of using a #pragma directive in C/C++ language −
#pragma token_name
Types of Pragma Directives in C
The table of some of #pragma directives in C/C++ language is given as follows,
Directive | Description |
---|---|
#pragma startup | Before the execution of main(), the function specified in pragma is needed to run. |
#pragma exit | Before the end of program, the function specified in pragma is needed to run. |
#pragma warn | Used to hide the warning messages. |
#pragma GCC dependency | Checks the dates of current and other file. If other file is recent, it shows a warning message. |
#pragma GCC system_header | It treats the code of current file as if it came from system header. |
#pragma GCC poison | Used to block an identifier from the program. |
#pragma once | Compiler loads the header file only once. |
#pragma startup and exit
These pragma directives are executed before and after the main() function. Not all the compilers support these directives.
Example
The following code demonstrates how you can use the pragma startup and exit directives −
#include <stdio.h> int display(); #pragma startup display #pragma exit display int main(){ printf("\nI am in main function"); return 0; } int display() { printf("\nI am in display function"); return 0; }
Output
When you run this code, it will produce the following output −
I am in main function
#pragma warn
The #pragma warn directive is used to hide or display the warning messages which are displayed during compilation.
The warn pragma is used as per the following syntax −
#pragma warn +xxx (To show the warning) #pragma warn -xxx (To hide the warning) #pragma warn .xxx (To toggle between hide and show)
The three character codes to be used are rvl (return value), par (parameter used or not), and rch (if the code is unreachable).
If any character code is prefixed by "+", it indicates to show the warning; prefixed by "–" means indication to the compiler to hide warnings, and prefix by dot (.) is an instruction to the compiler to toggle between hide and display warnings.
Example
The following example shows how you can use the warn pragma in a C program −
#include <stdio.h> #pragma warn -rvl /* return value */ #pragma warn +par /* parameter never used */ #pragma warn –rch /* unreachable code */ int square(int x){ printf("Hello World"); } int main(){ square(10); return 0; }
Output
When you run this code, it will produce the following output −
Hello World
#pragma GCC poison
The GCC compiler removes an identifier completely from the program. If we want to block an identifier, then we can use the #pragma GCC poison directive. Its syntax is as follows −
#pragma GCC poison identifier
Example
In this example, we will use the GCC poison pragma to block the printf() function −
#include <stdio.h> #pragma GCC poison printf int main(){ int a = 10; if (a == 10) { printf("Hello World"); } else printf("TutorialsPoint"); return 0; }
Output
When you try to compile this code, it will show the following error −
error: attempt to use poisoned "printf"
#pragma GCC dependency
This pragma 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.
Example
Take a look at the following example −
#include <stdio.h> #pragma GCC dependency "depends.c" int main(){ printf("Hello, World!"); return 0; }
Output
The above source code is depending on depends.c. If its compilation timestamp is more recent, then the following warning is issued −
warning: current file is older than depends.c
#pragma GCC system_header
As a convention, system header files are placed angular brackets in front of the #include directive, whereas the non-system header files are in quotation marks. If you want the compiler to treat the latter as the system header, use this pragma.
Syntax
#pragma GCC system_header
library.h
We define a "library.h" header file in the current directory.
#ifndef LIBRARY_H #define LIBRARY_H #pragma GCC system_header void myFunction(); #endif
Example
To ask the compiler to treat "library.h" as a system header, use the #pragma GCC system_header.
#include <stdio.h> #include "library.h" int main(){ myFunction(); // Using a function from the library.h printf("Hello, World!\n"); return 0; }
#pragma once
The #pragma once directive causes the header file to be included only once, even if the programmer includes it multiple times.
Save the "myheader.h" file as −
myheader.h
#pragma once void myFunction();
Example
In another code (main.c), call myFunction() as follows −
#include <stdio.h> #include "myheader.h" int main(){ myFunction(); printf("Hello, World!\n"); return 0; }