C Preprocessor Directives
The C preprocessor is a micro processor that is used by compiler to transform your code before compilation. It is called micro preprocessor because it allows us to add macros.
Note: Proprocessor direcives are executed before compilation.
All preprocessor directives starts with hash # symbol.
Let's see a list of preprocessor directives.
- #include
- #define
- #undef
- #ifdef
- #ifndef
- #if
- #else
- #elif
- #endif
- #error
- #pragma
C Macros
A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two types of macros:
- Object-like Macros
- Function-like Macros
Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. For example:
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros
The function-like macro looks like function call. For example:
Here, MIN is the macro name.
Visit #define to see the full example of object-like and function-like macros.
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
No. | Macro | Description |
---|---|---|
1 | _DATE_ | represents current date in "MMM DD YYYY" format. |
2 | _TIME_ | represents current time in "HH:MM:SS" format. |
3 | _FILE_ | represents current file name. |
4 | _LINE_ | represents current line number. |
5 | _STDC_ | It is defined as 1 when compiler complies with the ANSI standard. |
C predefined macros example
File: simple.c
Output:
File :simple.c Date :Dec 6 2015 Time :12:28:46 Line :6 STDC :1
C #include
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error.
By the use of #include directive, we provide information to the preprocessor where to look for the header files. There are two variants to use #include directive.
- #include <filename>
- #include "filename"
The #include <filename> tells the compiler to look for the directory where system header files are held. In UNIX, it is \usr\include directory.
The #include "filename" tells the compiler to look in the current directory from where program is running.
#include directive example
Let's see a simple example of #include directive. In this program, we are including stdio.h file because printf() function is defined in this file.
Output:
Hello C
#include notes:
Note 1: In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as filename.
Note 2: In #include directive, backslash is considered as normal text not escape sequence. So in case of #include <a\nb>, a\nb is treated as filename.
Note 3: You can use only comment after filename otherwise it will give error.
C #define
The #define preprocessor directive is used to define constant or micro substitution. It can use any basic data type.
Syntax:
Let's see an example of #define to define a constant.
Output:
3.140000
Let's see an example of #define to create a macro.
Output:
Minimum between 10 and 20 is: 10
C #undef
The #undef preprocessor directive is used to undefine the constant or macro defined by #define.
Syntax:
Let's see a simple example to define and undefine a constant.
Output:
Compile Time Error: 'PI' undeclared
The #undef directive is used to define the preprocessor constant to a limited scope so that you can declare constant again.
Let's see an example where we are defining and undefining number variable. But before being undefined, it was used by square variable.
Output:
225
C #ifdef
The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present.
Syntax:
Syntax with #else:
C #ifdef example
Let's see a simple example to use #ifdef preprocessor directive.
Output:
Value of a: 2
But, if you don't define NOINPUT, it will ask user to enter a number.
Output:
Enter a:5 Value of a: 5
C #ifndef
The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code otherwise #else code is executed, if present.
Syntax:
Syntax with #else:
C #ifndef example
Let's see a simple example to use #ifndef preprocessor directive.
Output:
Enter a:5 Value of a: 5
But, if you don't define INPUT, it will execute the code of #ifndef.
Output:
Value of a: 2
C #if
The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.
Syntax:
Syntax with #else:
Syntax with #elif and #else:
C #if example
Let's see a simple example to use #if preprocessor directive.
Output:
Value of Number is: 0
C #else
The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives.
Syntax:
Syntax with #elif:
C #else example
Let's see a simple example to use #else preprocessor directive.
Output:
Value of Number is non-zero
C #error
The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found and skips further compilation process.
C #error example
Let's see a simple example to use #error preprocessor directive.
Output:
Compile Time Error: First include then compile
But, if you include math.h, it does not gives error.
Output:
2.645751
C #pragma
The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating-system feature.
Syntax:
Different compilers can provide different usage of #pragma directive.
The turbo C++ compiler supports following #pragma directives.
Let's see a simple example to use #pragma preprocessor directive.
Output:
I am in func I am in main I am in func
No comments:
Post a Comment