0% found this document useful (0 votes)
37 views

Unit 1 Ppt Preprocessor

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Unit 1 Ppt Preprocessor

Uploaded by

Nan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

INTRODUCTION TO PREPROCESSOR

 Before a c program is compiled by a compiler, the source


code is processed by preprocessor.
 The preprocessor directives give instruction to the
compiler to preprocess the information before actual
compilation starts. All preprocessor directives begin with
#,
 Preprocessor is not a part of the compiler . But is a
separate step in the compilation process.
 It allows user to define macro and so called macro
preprocessor.
 It is a text substitution tool.
 Commands used in preprocessor are called
preprocessor directive.
 Preprocessor directive have separate set of rules from
normal c syntax.
 Begin with # and doesn’t end with;
PREPROCESSOR(CONT…)
SOME PREPROCESSOR COMMANDS ARE
#define
#undef
#include
#ifdef

Example:
# define MAX_LENGTH 20
#include ”headerfile.h”
#define square(X) ((X)*(X))
PREPROCESSOR Directives

 Macro substitution directive


 File inclusion directive
 Compiler control directive
MACRO SUBSTITUTION

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

SIMPLE MACRO SUBSTITUTION

#define NUMBER 100


# define TRUE 0
#define CITY “MUMBAI”
EXAMPLE 1
#define PI 3.14
#include<stdio.h>
int main()
{
int rad;
float area,cir;
printf("Enter the radios:");
scanf("%d",&rad);
printf("Area %f",PI*rad*rad);
printf("\nCircumference %f",2*PI*rad);
return 0;
}
EXAMPLE 2
#include<stdio.h>
#define NUM int
#define OUT printf
NUM main()
{
NUM a,b,c;
a=45;
b=25;
c=a+b;
OUT("Sum %d",c);
return 0;
}
EXAMPLE 3
#include<stdio.h>
#define PRINT printf("hi")
#define CHECK if(x>y)
void main()
{
int x=10,y=5;
CHECK PRINT; if(10>5) printf(“hi”);
}
Argumented Macro/Function
Macro substitution

 Preprocessor permits to define more complex


macros
 #define identifier(f1,f2,f3,,,fn)
 Similar to function definition
 Formal macro arguments=function definition
arguments
 Subsequent occurrence of macro with argument
known as macro call
EXAMPLE 1

#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

 List of preprocessor directives :


 #include
 #define
 #undef
 #ifdef
 #ifndef
 #if
 #else
 #elif
 #endif
 #error
 #pragma
Compiler control directives / Conditional
Compilation(cont..)

#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

 The preprocessor directive #pragma is used to provide the additional


information to the compiler.
#pragma startup
Before the execution of main(), the function specified in pragma is needed to
run.
#pragma exit
Before the end of program, the function
specified in pragma is needed to run.
#pragma warn
Used to hide the warning messages.
#pragma Example
#include<stdio.h>
int display();
#pragma startup display
#pragma exit display
int main()
{
printf("\nI am in main function");
return 0;
}
int display()
{
printf("\nI am in display function");
return 0;
}
COMPILATION PROCESS

 Compiling a C program is a multi-stage process. At an


overview level, the process can be split into four separate
stages: Preprocessing, compilation, assembly, and
linking.
Preprocessing

 The first stage of compilation is called preprocessing.


 In this stage, lines starting with a # character are
interpreted by the preprocessor as preprocessor
commands.
 These commands form a simple macro language with
its own syntax and semantics.
 This language is used to reduce repetition in source
code by providing functionality to inline files, define
macros, and to conditionally omit code.
 The preprocessed output is stored in the filename.i.
Compilation

 The second stage of compilation is called


compilation.
 In this stage, the preprocessed code is translated
to assembly instructions specific to the target
processor architecture.
 These form an intermediate human readable
language.
 compile filename.i and produce an; intermediate
compiled output file filename.s
Assembly
 During this stage, an assembler is used to translate the assembly
instructions to object code.
 The output consists of actual instructions to be run by the target
processor.
 In this phase the filename.s is taken as input and turned
into filename.o by assembler. This file contain machine level
instructions.
Linking
 The object code generated in the assembly stage is
composed of machine instructions that the processor
understands but some pieces of the program are out of
order or missing.
 To produce an executable program, the existing pieces
have to be rearranged and the missing ones filled in.
This process is called linking.

 The linker will arrange the pieces of object code so that


functions in some pieces can successfully call functions
in other ones.
 It will also add pieces containing the instructions for
library functions used by the program.
 The result of this stage is the final executable
Check whether the required amount
can be withdrawn
based on the available amount

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

You might also like