There are other C pre-processors like #if, #elif, #else, #endif
etc which are used for conditional compilation. If we want to skip over a part of code during compilation, we can use these pre-processors. This is mostly useful when we write code which depends on the operating system. Suppose, some code we have written is not applicable for Windows and only applicable for Linux machines, we can skip the code, when compiling from Windows machine using this pre-processors. The general form of #ifdef
is
- #ifdef macroname
- Statement block;
- #endif
The
'#ifdef'
stands for 'if defined'. It means that if the macro name is already defined somewhere, then the statement block will be included in the source for compilation, otherwise it will be skipped. The #ifdef
is accompanied by an #endif
statement, which marks the end of statement block. We can use #ifndef
, which is applicable when we want to compile part of program if the corresponding macro name is not defined. Like nested if...else
, #ifdef
also can be combined with #elif
and #else
. We can use them as given in the below example.Conditional Compilation Example in C
- #ifdef SYSTEM==LINUX
- statement block 1;
- #elif SYSTEM==WINDOWS
- statement block 2;
- #else
- statement block 3;
- #endif
Predefined Macros
Apart from the above mentioned macros, ANCI has a set of predefined macros which can be used for functionalities that's used commonly in application programs. They are explained below.
Macro | Description |
---|---|
__DATE__ | Gives current date in MMM DD YYYY format |
__TIME__ | Gives current time in HH:MM:SS format |
__FILE__ | Gives the current file name |
__LINE__ | Gives the current line number of source code |
__STDC__ | Gives 1 when the compilation follows ANSI standard |
No comments:
Post a Comment