You are on page 1of 40

UNIT-V

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.

Syntax: #ifdef, #endif, #if, #else, #ifndef


Set of commands are included or excluded in
Conditional compilation
source program before compilation with respect to
the condition.

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

Each occurrence of PI will be


PI 3.142
replaced by 3.142

TRUE 1 Each occurrence of TRUE will be


replaced by 1

Each occurrence of LESSTHAN will


LESSTHAN <
be replaced by >

Each occurrence of MESSAGE will


MESSAGE welcome to C
be replaced by welcome to c
Ex
#include <stdio.h>
#define PI 3.1415
int main()
{ float radius, area;
printf("Enter the radius: ");
scanf("%d", &radius);
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;}
Ex:2
#include <stdio.h>

#define height 100


#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '\?'

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

printf("MIN number : %d",MIN);


}
//main.c
#include <stdio.h>
main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
#if 5>10
printf("Sum %d",a+b);
#else
if(a==b)
printf("Eqauls");
else if(a>b)
printf("Biggest number %d",a);
else
printf("Biggest number %d",b);
#endif
}
Output
Enter two numbers:
45
30
Biggest number 45

In the example as 5>10 gives an integer constant 0


(false), the preprocessor sends the code between
#else and #endif to the compiler. Now we can realize
this by looking at it’s expanded source
/* main.i */
main.c 2: main()
main.c 3: {
main.c 4: int a,b;
main.c 5: printf("Enter two numbers:\n");
main.c 6: scanf("%d%d",&a,&b);
main.c 7:
main.c 8:
main.c 9:
main.c 10: if(a==b)
main.c 11: printf("Eqauls");
main.c 12: else if(a>b)
main.c 13: printf("Biggest number %d",a);
main.c 14: else
main.c 15: printf("Biggest number %d",b);
main.c 16:
main.c 17: }
What would be output of following program
#include <stdio.h>
#define A 10
#define B 40
int main()
{
#if A==B
printf("Hello");
#elif A>B
printf("World");
#else
printf(“Conditional Compilation");
#endif
return 0;
}
#ifndef statement
• These Conditional Compilation Directives
allow us to include certain portion of the code
depending upon the output of constant
expression.
• Block is Called as Conditional Group
EX
#include"stdio.h"

void main()
{
// Define another macro if MACRO NUM is defined

#ifndef NUM
#define MAX 20
#endif

printf("MAX number is : %d",MAX);


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 &lt;stdio.h&gt;  
#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.,

• The command line arguments are handled


using main() function arguments where argc
refers to the number of arguments passed,
and argv[] is a pointer array which points to
each argument passed to the program.
EX:1
#include <stdio.h>

int main( int argc, char *argv[] ) {

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");
}

printf("\n Program name : %s \n", argv[0]);


printf("1st arg : %s \n", argv[1]);
printf("2nd arg : %s \n", argv[2]);
printf("3rd arg : %s \n", argv[3]);
printf("4th arg : %s \n", argv[4]);
printf("5th arg : %s \n", argv[5]);
}
C Program to Add two numbers using
Command Line Arguments
#include<stdio.h>

void main(int argc, char * argv[]) {


int i, sum = 0;
if (argc != 3) {
printf("You have forgot to type numbers.");
exit(1); //exit() terminates the calling process without executing the rest code
}

printf("The sum is : ");

for (i = 1; i < argc; i++)


sum = sum + atoi(argv[i]); /*atoi() function in C language converts string data type to int data
type*/
printf("%d", sum);

}
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.,

• Inside Command Prompt type this command.


add 10 20
• Hit Enter , You will get following Output.
Copy Text From One File to Other File
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
void main() {
   FILE *fp1, *fp2;
   char ch;
   clrscr();
 
   fp1 = fopen("Sample.txt", "r");
   fp2 = fopen("Output.txt", "w");
   
while (1) {
      ch = fgetc(fp1);
 
      if (ch == EOF)
         break;
      else
         putc(ch, fp2);
   }
 
   printf("File copied Successfully!");
   fclose(fp1);
   fclose(fp2);
}
• “fgetc” will read character from source file.
• Check whether character is “End Character of
File” or not , if yes then Terminate Loop
• “putc” will write Single Character on File
Pointed by “fp2” pointer

You might also like