Separate Step in The Compilation Process: Substitution Tool Pre-Processing
Separate Step in The Compilation Process: Substitution Tool Pre-Processing
Preprocessor Directives
Syntax: #undef, #pragma
#undef is used to undefine a defined macro
Other directives
variable. #Pragma is used to call a function before
and after main function in a C program.
Sr.No. Directive & Description
1 #define
Substitutes a preprocessor macro.
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:
MIN number : 100
#else
#define MIN 100
#endif
void main()
{
// Define another macro if MACRO NUM is defined
#ifndef NUM
#define MAX 20
#endif
#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.,