×

#error #pragma in C

#error, #pragma in C

#error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which will appear in the list of the other compilation errors.

Error directive is most helpful when combined with the #if or #else or #elif statements to fail the compilation if at all some condition is not true, the #error is generally used along with it as a safety check during the compilation process.

In other words, the #error directive emits a user specified error message at the compile time which then will terminate the whole compilation. It shows the given error in the program to the user and notifies that the following program or application is not formed according to the standard.

The token string parameter is included in the error message that will be emitted from the error directive. This token string parameter is not subjected to any expansion that includes macro. The error directive is most helpful while preprocessing the programs or applications, which instantly notifies the programmer about the inconsistency or the violation happening within the application during the preprocessing stage.

The error directive present in the source file prevents the code generation; if a section of that program or application has reached that should be bypassed.

The #error preprocessor directive indicates the error. The compiler gives a fatal error if #error directive is found and skips further compilation process.

Syntax:

             #error token string
                         Or
             #error error message 

After the compiler encounters the #error directive, the compiler displays the message of the implementation of the “error message” or “token string”. It renders the program is ill formed. “error message” or “token string” consists of a few to several words which will be user defined.

E.g.:

 #ifndef __unix__/*__unix__ is typically defined when targetting Unix*/
 #error "Only Unix is supported"
 #endif 
 #define BUFFER_SIZE 255
 #if BUFFER_SIZE < 256
 #error "The BUFFER_SIZE is too small."
 #endif 

Output:

The BUFFER_SIZE is too small.
 #if __STDC__ != 1
 #  error "Not a standard compliant compiler"
 #endif
 #include <stdio.h>
 #include <conio.h>
 int main (void)
 {
     printf("The compiler that is used conforms to the ISO C Standard !!");
 } 

Output:

            The compiler that is used conforms to the ISO C Standard !!
 #include<stdio.h> 
 #ifndef __MATH_H 
 #error First include then compile 
 #else 
 void main(){ 
     float a; 
     a=sqrt(7); 
     printf("%f",a); 
 } 
 #endif  

Output:

Compile time error: First include then compile

#pragma in C

The #pragma preprocessor directive is used to provide additional information to the compiler. The compiler uses the #pragma directive to offer a machine or operating-system features. The preprocessor directive, that is, #pragma, is generally used to provide the extra information to the compiler present in the C standard. It is always used by the compiler in order to give the extra features that are required.

Syntax:

            #pragma token string
  1. #pragma startup: Before the execution of main (), the function specified in pragma is needed to run.
  2. #pragma exit: Before the end of the program, the function specified in pragma is needed to run.
  3. #pragma warn: Used to hide the warning messages.
  4. #pragma GCC dependency: Checks the dates of current and another file. If another file is recent, it shows a warning message.
  5. #pragma GCC system_header: It treats the code of the current file as if it came from the system header.
  6. #pragma hdrfile 
  7. #pragma argsused  
  8. #pragma option 
  9. #pragma saveregs  
  10. #pragma hdrstop 
  11. #pragma inline 

E.g.:

 #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:

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

Output:

Tutorials And Examples
  • #pragma warn -rvl: This directive hides those warning which is 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 is raised when a function does not use the parameters passed to it.
  • #pragma warn -rch: This directive hides those warnings raised when a code is unreachable. For example, any code written after the return statement in a function is unreachable.

Related Topics

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.