#error, #pragma and #null Directives in C

This C Tutorial explains #error, #pragma and #null Directives in C and How are they Used in C Programs.

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

advertisement
advertisement
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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.

advertisement

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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.