Unit 1 Ppt Preprocessor
Unit 1 Ppt Preprocessor
Example:
# define MAX_LENGTH 20
#include ”headerfile.h”
#define square(X) ((X)*(X))
PREPROCESSOR Directives
Macro Definition/Macro(SYNTAX)
#define identifier string
……
……
Identifier
……
Identifier
……
Preprocessor replace all the identifier with string in
the source code before compilation
Types of macro substitution
Simple
Argumented
Nested
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of rectangle is: %d", area);
return 0;
}
EXAMPLE 2
#include<stdio.h>
#define SQU(x) (x*x)
int main()
{
int x;
float y;
x = SQU(3);
y = SQU(3.1);
printf("\nSquare of Integer : %d",x);
printf("\nSquare of Float : %f",y);
return(0);
}
DIFFERENCE BETWEEN MACRO
AND FUNCTION
#include<stdio.h>
#define SQUARE(X) X*X
int square(int a)
{
return(a*a);
OUTPUT:
} macro:9
function:9
void main()
{
int b=3;
printf(“macro:%d\n”,SQUARE(b));
printf(“function:%d\n”,square(b));
}
EXAMPLE
include<stdio.h>
#define SQUARE(X) X*X
int square(int a)
{
return(a*a);
OUTPUT:
} macro::11
void main() Function:25
{
int b=3,c=2;
printf(“macro:%d\n”,SQUARE(b+c));
//3+2*3+2=3+6+2=11
printf(“function:%d\n”,square(b+c));
Nesting of macros
One macro in another macro
Macro2
{
……
Macro1
……
}
#define SQUARE(X) ((X)*(X))
#define CUBE (SQUARE(X)*(X))//NESTING
Replaced as,
#define CUBE((X)*(X)*(X))
EXAMPLE
#include<stdio.h>
#define SQU(x) ((x)*x)
#define CUBE(x) (SQU(x)*x)
int main()
{
int x;
int y;
x = SQU(3); // Argumented Macro
y = CUBE(3); // Nested Macro
printf("\nSquare of 3 : %d",x);
printf("\nCube of 3 : %d",y);
return(0);
}
PREDEFINED MACRO IN C
_LINE_ :macro contains the current line number of the program in the
compilation.
_FILE_: macro holds the file name of the currently executing program in the
computer.
_DATE_: macro gives the date at which source code of this program is
converted into object code.
_TIME_: macro gives the time at which program was compiled. Time is in the
format hour:minute:second.
_STDC_: Macro is used to confirm the compiler standard. Generally it holds
the value 1 which means that the compiler conforms to ISO Standard C.
EXAMPLE
#include <stdio.h>
int main()
{
printf("Time of compilation is: %s\n", _TIME_);
return 0;
}
Output:
Time of compilation is: 13:17:20
FILE INCLUSION
External file or functions containing macro definitions and
functions can be included in the program by preprocessor
directives.
When a program becomes very large, it is good practice to divide
it into smaller files and include whenever needed. These types of
files are user defined files. These files can be included as:
#include”filename”
#include<Filename>
where file_name is the name of file to be included. The ‘<‘ and ‘>’
brackets tells the compiler to look for the file in standard directory.
//Only in standard directory
Compiler control directives / Conditional
Compilation
#if expression
// conditional codes
#endif
#if expression
// conditional codes if expression is non-zero
#elif expression1
// conditional codes if expression is non-zero
#elif expression2
// conditional codes if expression is non-zero
#else
// conditional if all expressions are 0
#endif
#pragma
#include<stdio.h>
void main()
{
double amount=25000.50,withdraw;
printf(“Enter the amount to withdraw”);
scanf(“%lf”,&withdraw);
if(amount>=withdraw)
printf(“You can withdraw”);
else
printf(“Insufficient balance”);
}
sum of all even numbers between 1 to n
/** * C program to print sum of all even numbers between 1 to n */
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &n);
for(i=2; i<=n; i+=2)
{
/* Add current even number to sum */
sum += i;
}
printf("Sum of all even number between 1 to %d = %d", n, sum
Menu-driven program to find the area of different shapes
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radius of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break ;
case 2:
printf("Input length and breadth of the rectangle :
");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and height of the triangle :");
scanf("%d%d",&b,&h);
area=0.5*b*h;
break;
}
printf("The area is : %f\n",area);
}