×

#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

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.