
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- 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
- C - Predefined Identifier __func__
- 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
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- 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 - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Memory Leaks
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- 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
- C Online Compiler
#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; }