#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
- #pragma startup: Before the execution of main (), the function specified in pragma is needed to run.
- #pragma exit: Before the end of the 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 another file. If another file is recent, it shows a warning message.
- #pragma GCC system_header: It treats the code of the current file as if it came from the system header.
- #pragma hdrfile
- #pragma argsused
- #pragma option
- #pragma saveregs
- #pragma hdrstop
- #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.