Introduction:
- Pre-processor program that modifies the text of a C program before it is compiled.
- Pre-processing includes
- Inclusion of other files
- Definition of symbolic constants and macros
- Conditional compilation of program code
- Conditional execution of preprocessor directives
Preprocessing, Compiling and Linking:
one.c –> PREPROCESSOR -> tmp.c(temporary) -> COMPILER -> one.obj -> LINKER -> one.exe (final Executable) |
Inclusion of header files:
- C application is a collection of Source files(.c) and Header files(.h)
- #include directive is used to connect the files in C application
We include Library file as well as Custom defined Files:
#include <filename>
- Searches standard library for file
- Use for standard library files
#include “filename”
- Searches current directory, then standard library
- Use for user-defined files
if you have a header file ‘header.h’ as follows,
int x; int biggest(int x, int y) { retrun x>y?x:y; } |
and a main program called ‘program.c’ that uses the header file, like this,
#include “header.h” int main (void) { x=biggest(10,20); printf(“big :%d”,x); return 0; } |
Macros(#define):
- We use #define to create Macro constants.
- Constants recommended to define in Upper case.
- At the time or pre-processing all Macros replaced with their values
Syntax: #define identifier replacement-text Example: #define PI 3.14159 |
Function-like Macros
- You can also define macros looks like a function call.
- For example,
#define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
- Would cause
area = CIRCLE_AREA( 4 );
- To become
area = ( 3.14159 * ( 4 ) * ( 4 ) );
Conditional Compilation (#if, #ifdef, #ifndef, #else, #elif, #endif)
- Six directives are available to control conditional compilation.
- They delimit blocks of program text that are compiled only if a specified condition is true.
#if directive:
- It is conditional compilation directive.
- If the condition is valid then the code defined inside the block gets compiled.
Syntax:
#if <Constant_expression> ————- ————- #endif |
If constant expression will return 0 then condition will FALSE if it will return any non-zero number condition will TRUE.
#include<stdio.h> #if 0 int main() { printf(“HELLO WORLD”); return 0; } #endif Runtime Error: undefined symbol main |
#else directive:The code which is defined in else-block gets compiled if the condition fails.
#include<stdio.h> #if(-4) int main() { printf(“WELCOME TO C-WORLD “); return 0; } #else int main() { printf(“HELLO WORLD”); return 0; } #endif Output: WELCOME TO C-WORLD |
Program to display System date using pre-defined Macro constant:
#include<stdio.h> int main() { #ifdef __DATE__ printf(“%s”,__DATE__); #else printf(“First define the __DATE__”); #endif return 0; } |
Output: It will print current system date.
Explanation: __DATE__ is global identifier. It has already defined in the header file stdio.h and it keeps the current system date.
Program to display System Time using pre-defined Macro
#include<stdio.h> #define I 30 int main() { #ifndef __TIME__ printf(“%d”,I); #else printf(“%s”,__TIME__); #endif return 0; } |
Output: It will print current system time.
Explanation: __TIME__ is global identifier. It has been defined in the header file stdio.h. Compiler doesn’t compile the c codes which are inside the any conditional preprocessor directive if its condition is false. So we can write anything inside it.