Preprocessor Directives: Separate Step in The Compilation Process
Preprocessor Directives: Separate Step in The Compilation Process
Preprocessor Directives
• The C Preprocessor is not a part of the
compiler, but is a separate step in the
compilation process.
• In simple terms, a C Preprocessor is just a text
substitution tool and it instructs the compiler
to do required pre-processing before the
actual compilation.
• All preprocessor commands begin with a hash
symbol (#).
Preprocessor Syntax/Description
Syntax: #define
Macro This macro defines constant value and can be
any of the basic data types.
Syntax: #include <file_name>,
#include"filename"
Header file inclusion The source code of the file “file_name” is
included in the main program at the specified
place.
2 #include
Inserts a particular header from another file.
3 #undef
Undefines a preprocessor macro.
4 #ifdef
Returns true if this macro is defined.
5 #ifndef
Returns true if this macro is not defined.
6 #if
Tests if a compile time condition is true.
7 #else
The alternative for #if.
8 #elif
#else and #if in one statement.
9 #endif
Ends preprocessor conditional.
10 #error
Prints error message on stderr.
11 #pragma
Issues special commands to the compiler, using a standardized
method.
File Inclusion
• The #include preprocessor is used to include
header files to a C program. For example,
#include <stdio.h>
• You can also create your own header file
containing function declaration and include it
in your program using this preprocessor
directive.
#include "my_header.h"
Macro
• In C Programming, we can define constants using
the #define preprocessor directive.
• It is also called as simple substitution macro as it
simply removes the occurrences of the constant
and replace them using the expression.
#define PI 3.142
#define TRUE 1
#define AND &&
#define LESSTHAN <
#define MESSAGE "welcome to C"
Constant Value of constant Expression
void main()
{
printf("value of height : %d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
Output
value of height : 100
value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?
Conditional compilation
• Conditional compilation as the name implies
that the code is compiled if certain
condition(s) hold true.
• Normally we use if keyword for checking some
condition so we have to use something
different, so that compiler can determine
whether to compile the code or not. The
different thing is #if.
Ex:1
#include <stdio.h>
#define x 10
void main()
{
#ifdef x
printf("hello\n"); // this is compiled as x is defined
#else
printf("bye\n"); // this is not compiled
#endif
}
EX:2
#include <stdio.h>
int main()
{
#define COMPUTER "An amazing device"
#ifdef COMPUTER
printf(COMPUTER);
#endif
return 0;
}
Ex:3
#include<stdio.h>
void main()
{
#ifdef MAX
#define MIN 90 Output:
#else MIN number : 100
#define MIN 100
#endif
void main()
{
// Define another macro if MACRO NUM is defined
#ifndef NUM
#define MAX 20
#endif
} Output :
MAX Number is 20
What will be the output of the
program?
#include<stdio.h>
#define SQR(x)(x*x)
int main() {
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
return 0; }
Explanation
The macro function SQR(x)(x*x) calculate the
square of the given number 'x'. (Eg: 102)
Step 1: int a, b=3; Here the variable a, b are
declared as an integer type and the variable b is
initialized to 3.
Step 2: a = SQR(b+2); becomes,
=> a = b+2 * b+2; Here SQR(x) is replaced by
macro to x*x .
=> a = 3+2 * 3+2;
=> a = 3 + 6 + 2;
=> a = 11;
What will be the output of the
program?
#include <stdio.h>
#define AREA(l, b) (l * b)
void main()
{
int l= 10, b= 10, area;
area = AREA(l,b);
printf( “Area of rectangle is: %d”; area);
}
Command Line Argument
• It is possible to pass some values from the
command line to your C programs when they
are executed.
• These values are called command line
arguments and many times they are
important for your program especially when
you want to control your program from
outside instead of hard coding those values
inside the code.
Command Line Arguments Contd.,
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
• It should be noted that argv[0] holds the
name of the program itself and argv[1] is a
pointer to the first command line argument
supplied, and *argv[n] is the last argument. If
no arguments are supplied, argc will be one,
and if you pass one argument then argc is set
at 2.
EX:2
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[]) // command line arguments
{
if(argc!=5)
{
printf("Arguments passed through command line " \
"not equal to 5");
}
}
Sample Questions
#include<stdio.h>
void f();
int main()
{
#define foo(x, y) x / y + x
f();
}
void f()
{
printf("%d\n", foo(-4,4 ));
}
Outpot :-5
#include<stdio.h>
#define SYSTEM 500
void main(){
int a = 500;
#if SYSTEM == a
printf(“Electronics ");
#endif
#if SYSTEM == 500
printf(“Communication\n");
#endif}
Output:Communication
#include <stdio.h>
#define SQR(x)(x*x)
main(){
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
}
Output:11
Steps to be followed to execute program using
Command Line Argument inside TC C/C++
Compiler
Contd.,
void main() {
FILE *fp1, *fp2;
char ch;
clrscr();
if (ch == EOF)
break;
else
putc(ch, fp2);
}