#error directive let’s you display error massage for some test condition. For ex.,
/* err_dir.c-- program shows a way to use #error directive */ #include <stdio.h> /* YES and NO defined */ #define YES #define NO int main(void) { /* let's test if YES and NO are defined or not */ /* conditional compilation */ #ifdef YES #ifdef NO puts("Both YES and NO defined!"); #else puts("YES defined!"); #endif /* NO */ #elif NO puts("NO defined!"); #else #error None of YES and NO defined! #endif /* YES */ return 0; }
Observe below output of preprocessor excluding the library files’ text,
int main(void) { puts("Both YES and NO defined!"); return 0; }
and output of the program when compiled and run as,
Both YES and NO defined!
Let’s now turn off YES and NO using #undef as follows
#undef YES #undef NO
and recompile and run the program, output produced as
error: #error None of YES and NO defined!
#pragma
Modern compilers have several settings that can be modified by command-line arguments or by using an IDE menu. The #pragma lets you place compiler instructions in the source code. Generally, each compiler has its own set of pragmas. They might be used, for example, to control the amount of memory set aside for automatic
variables or to set the strictness of error checking or to turn listings on or off during compilation or to insert assembly code into C programs.
Pragmas are inherently non portable. Unrecognised #pragmas are ignored by preprocessor, and two different compilers might interpret the same #pragma in different ways.
# or null directives
The null directive is a line with a pound sign but contains nothing else. These directives are simply deleted by the preprocessor. Let’s see an ex.,
/* null directives */ # #include "local_include.h" #
Because preprocessor deletes null directives, lines containing them remained as blank lines.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C Books