UNIT-5 (C PREPROCESSOR & BITWISE OPERATORS)
(RAM GOPAL GUPTA- http://ramgopalgupta.com/)
PART-1
C Preprocessor:
The C preprocessor is not a part of the compiler but is a separate step in compilation process.
The preprocessor is a program that processes the source program before it is passed to the
compiler. A ‘C’ preprocessor is a collection of special statements called directives that are
executed before the starting of the compilation process. All preprocessor commands begin with a
hash symbol (#).
C Program Processed .obj files
Preprocessor Preprocessor
C Program
The preprocessor directives can be divided into three categories:
1. Macro substitution
2. File inclusion
3. Conditional compilation
1. Macro Substitution:
Macro substitution has a name and replacement text, defined with #define directive. The
preprocessor simply replaces the name of macro with replacement text from the place where
the macro is defined in the source code. Macro substitution is commonly used to define
symbolic constants.
Examples of macro:
#define PI 3.14
#define X 100
#define P printf
Program example1:
/* Macro substitution */
Line 1- #include<stdio.h>
Line 2- #define NUM int Explanation:
Line 3- #define OUT printf Macro has been defined at Line 1 & 2
Line 4- NUM main() named “NUM” & “OUT”.
Line 5- { “NUM” substitute int
Line 6- NUM a,b,c; “OUT” substitute printf
Line 7- a=45; In line 4, 6 “NUM” is used in place of
int.
Line 8- b=25;
In line 10 “OUT” is used in place of
Line 9- c=a+b; printf.
Line 10- OUT("Sum %d",c);
Line 11- return 0;
Line 12- }
Program example2:
/* Macro substitution */
Line 1- #define PI 3.14
Line 2- #include<stdio.h>
Line 3- int main()
Explanation:
Line 4- {
Macro has been defined at Line1 “PI”.
Line 5- int rad;
“PI” substitute the value 3.14
Line 6- float area,cir; In line 9, “PI” is used in place of 3.14
Line 7- printf("Enter the radios:");
Line 8- scanf("%d",&rad);
Line 9- printf("Area %f",PI*rad*rad);
Line 10- printf("\nCircumference %f",2*PI*rad);
Line 11- return 0;
Line 12- }
Program example3:
/* Macro substitution */
Line 1- #include<stdio.h>
Line 2- #define SQUARE(x) (x * x)
Line 3- int main()
Line 4- {
Line 5- int num;
Line 6- printf("Enter the number:");
Line 7- scanf("%d",&num);
Line 8- printf("Square of %d is %d\n",num, SQUARE(num));
Line 9- return 0;
Line 10- }
Explanation:
Macro has been defined at Line 2 “SQUARE(x) (x * x)”.
“SQUARE(x) (x * x)” substitute the process of (x * x) i,e, square of a given number
In line 8, “SQUARE(x) (x * x)” is used to calculate the square of value store into
variable num;
2. File Inclusion:
It is the second important preprocessor directive. It helps to import and attach the contents of one
file into another automatically by the preprocessor. It is generally used on two purposes.
If we have a very large program, then it is a better programming practice to divide different
sections of the program as different files and linked together.
If we have some commonly used functions, macros and constants then these can be defined
as a separate file and then can be included into any program as we need.
We specify the file name with #include directive then the preprocessor automatically includes
the contents of header file into the program (as simple as copy and paste)
Syntax:
#include statement can be written in two forms, that are
1. #include "file name"
and
2. #include <file name>
Program example4:
/* File use.c */
Line 1- #define NUM 20
Line 2- #include<stdio.h>
Line 3- void show(){
Line 4- printf(“void show() called from use.c file”);
Line 5- }
/* File Inclusion example: finclusion.c */
Line 1- #include “use.c”
Line 2- int main()
Line 3- {
Line 4- printf("int main() called from finclusion.c file\n");
Line 5- printf(“NUM = %d\n”,NUM);
Line 6- show();
Line 7- return 0;
Line 8- }
Explanation:
In the above program example 4: there are two source code files. One is “use.c” another
is “finclusion.c”.
In source code “use.c”, we have created a Macro names “NUM” and a function
“show()”. Included “stdio.h” because we are using printf(…) inside show() function.
In source code “finclusion.c”: at line 1, we have used the file inclusion, included file
“use.c” therefore code of “use.c” is included into “finclusion.c” file. No need to write
code of header file (stdio.h), macro “NUM” and function “show()” in file “finclusion.c”
but we can call them as we did at line 5 & 6.
3. Conditional Compilation:
As the name implies, it is the process of selecting the source code conditionally from the
program and sending to the compiler using preprocessor statements like #if, #ifdef, #ifndef,
#else, #elif, #endif.
#if, #else & #endif statement:
Here the preprocessor selection statement #if checks the condition. If it is true or a non-zero then
statements between #if and #else are sent to the compiler. If it is false or a zero then statements
between #else and #endif are sent to the compiler.
Program example5:
/* File main.c */
Line 1- #include <stdio.h> Explanation:
Line 2- int main() In the example as 5>10 gives an integer constant 0
Line 3- { (false), the preprocessor sends the code between
Line 4- int a,b; #else and #endif to the compiler. Now we can
Line 5- printf("Enter two numbers:\n"); realize this by looking at it’s expanded source
Line 6- scanf("%d%d",&a,&b); /* main.c */
Line 7- #if 5>10 main.c 2: int main()
main.c 3: {
Line 8- printf("Sum %d",a+b); main.c 4: int a,b;
Line 9- #else main.c 5: printf("Enter two numbers:\n");
Line 10- if(a==b) main.c 6: scanf("%d%d",&a,&b);
main.c 7:
Line 11- printf("Eqauls"); main.c 8:
Line 12- else if(a>b) main.c 9:
Line 13- printf("Biggest number %d",a); main.c 10: if(a==b)
main.c 11: printf("Eqauls");
Line 14- else main.c 12: else if(a>b)
Line 15- printf("Biggest number %d",b); main.c 13: printf("Biggest number %d",a);
main.c 14: else
Line 16- #endif main.c 15: printf("Biggest number %d",b);
Line 17- return 0; main.c 16:
Line 18- } main.c 17: return 0;
main.c 18: }
Output:
Enter two numbers:
45
30
Biggest number 45
#ifdef, #ifndef preprocessor conditional statements:
#ifdef checks whether a macro with the name is defined or not. It returns true if in case macro
with the name is defined otherwise returns false.
#ifndef is opposite of #ifdef.
Here if the MACRO is defined with #define statement within the scope then code between #ifdef
and #else is send to the compiler otherwise statements between #else and #endif are send to the
compiler.
Here statements between #ifndef and #else are send to the compiler if the MACRO is not
defined, statements between #else and #endif are send to the compiler if MACRO is defined
within the scope.
Program example6:
Line 1- #include <stdio.h>
Line 2- #define MAX
Line 3- int main() Output:
Line 4- { Hello
Line 5- #ifdef MAX Fox
Line 6- printf("Hello");
Line 7- #else Explanation:
In this program as the macro MAX is defined
Line 8- printf("World"); “printf(“Hello”);” is sent to the compiler.
Line 9- #endif
Line 10- printf("\n"); As the macro MIN is not defined “printf(“Fox”);”
Line 11- #ifndef MIN is sent to the compiler
Line 12- printf("Fox");
Line 13- #else
Line 14- printf("Duck");
Line 15- #endif
Line 16- return 0;
Line 17- }