2. INTRODUCTION
C program execution steps are as
follows:
(i) the C program is written in
editor, and then
(ii) compilation,
(iii) linking and
(iv) the executable code
generation are to be done.
In between these stages, there also
involves one more stage i.e.
preprocessor.
The preprocessor is a program that
processes the source program, before it
is passed to the compiler.
4. INTRODUCTION (cont..)
The program typed in the editor is the source code to the
preprocessor.
The preprocessor then passes the source code to the compiler.
It is not necessary to write the program with the preprocessor
facility.
But preprocessor can reduce the execution time of a program,
because it takes place of a function call.
5. INTRODUCTION (cont..)
One of the most important features of the C language is to offer
preprocessor directives.
The preprocessor directives are always preferably initialized at the
beginning of the program before the main().
It begins with a symbol #(hash).
It can be placed anywhere but quite often, it is declared at the
beginning before the main() function or any particular function.
6. THE #DEFINE DIRECTIVE
The syntax of the #define directive is as follows:
#define identifier substitute
OR
#define identifier(argument 1… argument N) substitute
7. THE #DEFINE DIRECTIVE (cont..)
Example:
#define PI 3.14
This statement defines PI as macro templates and 3.14 as macro
substitute.
During preprocessing, the preprocessor replaces every occurrence
of PI (identifier) with 3.14 (substitute value).
Here, PI is a macro template and 3.14 is its macro expansion.
8. THE #DEFINE DIRECTIVE (cont..)
The macro templates are generally declared with capital letters for
quick identification.
One can also define macros with small letters.
The macro templates and its expansions must be separated with at
least one blank space.
It is not necessary to provide space between # and define.
9. THE #DEFINE DIRECTIVE (cont..)
The macro definition should not be terminated with a semi-colon.
The words followed by # are not keywords.
The programmer can use these words for variable names.
10. Use the identifier for 3.14 as PI and write a program to find the area
of circle using it.
# define PI 3.14
void main()
{
float r,area;
clrscr();
printf(“n Enter radius of the circle in cm :-”);
scanf(“%f”,&r);
area=PI*r*r;
printf(“Area of the Circle = %.2f cm^2”,area);
getche();
}
11. Example
Write a program to define and create identifier for C statements and
variables.
1. Read N as 10
2. Replace clrscr() with cls
3. Replace getche() with wait()
4. Replace printf with display
13. Write a program to define macros for logical operators.
# define and &&
# define equal ==
# define larger >
void main()
{
int a,b,c;
clrscr();
printf(“Enter Three Numbers. :”);
scanf(“%d %d %d”,&a,&b,&c);
if(a larger b and a larger c)
printf(“%d is larger than other two numbers.”,a);
else
if(b larger a and b larger c)
printf(“%d is larger than other two numbers.”,b);
else
if(c larger a and c larger b)
printf(“%d is the larger than other two numbers.”,c);
14. Cont..
else
if(a equal b and b equal c)
printf(“n Numbers are same.”);
}
Output:
Enter Three Numbers:7 8 4
8 is larger than other two numbers.
15. Write a program to create identifier for displaying double and triple
of a number.
# define DOUBLE(a) a*2
# define TRIPLE(a) a*3
void main()
{
int a=1;
clrscr();
printf(“nSINGLEtDOUBLEtTRIPLE”);
for(;a<=5;a++)
printf(“n%dt%dt%d”,a,DOUBLE(a),TRI
PLE(a));
getche();
}
SINGLE DOUBLE TRIPLE
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
16. UNDEFINING A MACRO
A macro defined with #define directives can be undefined with #undef
directive.
Syntax:
#undef macro_template substitute
It is useful, when we do not want to allow the use of macros in any portion
of the program.
17. Write a program to undefine a macro.
# define wait getche()
void main()
{
int k;
# undef wait getche();
clrscr();
for(k=1;k<=5;k++)
printf(“%dt”,k);
wait;
}
18. Cont..
In the program #undef directive undefines the same macro. Hence,
the compiler
flags an error message ‘undefined symbol ‘wait’ in function main()’.
19. Write a program to find the largest out of two numbers using macro
with arguments.
# define MAX(x,y) if (x>y) c=x; else c=y;
void main()
{
int x=5,y=8,c;
clrscr();
MAX(x,y);
printf(“n Largest out of two numbers = %d”,c);
getche();
}
20. THE #INCLUDE DIRECTIVE
The #include directive loads specified file in the current program.
The macros and functions of loaded file can be called in the current
program.
The included file also gets complied with the current program.
The syntax is as given below:
# include “filename”
# include <filename>
22. Write a program to show the use of #include directive:
Make two separate .c files as given below.
Main Program:-
#include <stdio.h>
#include "arith.c"
void main()
{
clrscr();
printf("sum(10, 20) = %dn", sum(10, 20));
getch();
}
arith.c file :-
int sum (int x, int y)
{
return (x + y);
}
23. Cont..
Output:-
sum(10, 20) =30
Even if sum function is not defined in main program we get result because we
have use #include directive to use sum function defined in “arith.c” file.
24. THE #LINE DIRECTIVE
In order to renumbering the source text this directive is used. The syntax of
line directive is as follows:
#line <constant> [ <identifier> ]
This causes the compiler to renumber the line number of the next source
line as given by <constant> and <identifier> gives the current input file.
25. Write a program to demonstrate #line directive (Run this program
by changing #line 15 statement with #line 20 or any other number
and understand the change in output.)
#include<stdio.h>
#include<conio.h>
#line 15
void main()
{
clrscr();
printf("ACPn");
printf("Line Number %dn",__LINE__);
printf("2nd Sem");
getch();
}
OUTPUT:
ACP
Line Number 19
2nd Sem
Line number is printed 19 because line
following the statement “#line 15” which
is “void main() “ will be renumbered as
line number 15 so the statement
“printf("Line Number %dn",__LINE__);” is
in Line number 19
26. THE PREDEFINED MACROS IN ANSI AND TURBO-C
Predefined Macros Function
_ _DATE_ _ Displays system date in string format.
_ _TIME_ _ Displays system time in string format.
_ _LINE_ _ Displays line number as an integer.
_ _FILE_ _ Displays current file name in string format.
27. Write a program to use predefined macros of ANSI
C/TURBO C.
void main()
{
clrscr();
printf(“nDATE: %s”,_ _DATE_ _);
printf(“nTIME: %s”,_ _TIME_ _);
printf(“nFILE NAME : %s”,_ _FILE_ _);
printf(“nLINE NO. : %d”,_ _LINE_ _);
}
DATE : March 11 2020
TIME : 21:07:39
FILE NAME : PRE_MA~1.C
LINE NO. : 8
28. STANDARD I/O PREDEFINED STREAMS
(Macros) IN STDIO.H
The predefined streams automatically open when the program is started.
Macros Function Definition in stdio.h
stdin Standard input device. #define stdin (&_streams[0])
stdout Standard output device. #define stdout (&_streams[1])
stderr Standard error output device. #define stderr (&_streams[2])
stdaux Standard auxiliary device. #define stdaux (&_streams[3])
stdprn Standard printer. #define stdprn (&_streams[4])
29. Write a program to enter text and display it using macro
expansions.
void main()
{
char ch[12];
int i;
clrscr();
printf(“Input a Text : ”);
for(i=0;i<11;i++)
ch[i]=getc(&_streams[0]);
printf(“Text Inputted : ”);
for (i=0;i<11;i++)
putc(ch[i],&_streams[1]);
}
Input a Text: Programming
Text Inputted : Programming
30. THE PREDEFINED MARCOS IN CTYPE.H
The header file ‘ctype.h’ contains a set of macros that check characters.
Macro Returns True Value If
isalpha(d) d is a letter.
isupper(d) d is a capital letter.
islower(d) d is a small letter.
isdigit(d) d is a digit.
isalnum(d) d is a letter or digit.
isxdigit(d) d is a hexadecimal digit.
isspace(d) d is a space.
ispunct(d) d is a punctuation symbol.
isprint(d) d is a printable character.
31. THE PREDEFINED MARCOS IN CTYPE.H
(cont..)
Macro Returns True Value If
isgraph(d) d is printable, but not be a space.
iscntrl(d) d is a control character.
isascii(d) d is an ASCII code.
32. Write a program to identify whether the entered character is a letter
or digit and capital or small using predefined macros.
# include<ctype.h>
void main()
{
char d;
int f;
clrscr();
printf(“n Enter any character : ”);
d=getche();
f=isalpha(d);
if(f!=0)
{
printf(“n%c is a letter in”,d);
f=isupper(d);
if (f!=0)
printf(“ Capital case”);
else
printf(“ Small Case”);
}
else
{
f=isdigit(d);
if(f!=0)
printf(“n %c is a digit”,d);