SlideShare a Scribd company logo
1 of 33
Unit-IV
PREPROCESSOR DIRECTIVES
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.
INTRODUCTION (cont..)
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.
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.
THE #DEFINE DIRECTIVE
 The syntax of the #define directive is as follows:
#define identifier substitute
OR
#define identifier(argument 1… argument N) substitute
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.
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.
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.
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();
}
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
Example (cont..)
# define N 10
# define cls clrscr()
# define wait() getche()
# define display printf
void main()
{
int k;
cls;
for(k=1;k<=N;k++)
display(“ %d”,k);
wait();
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
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);
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.
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
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.
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;
}
Cont..
 In the program #undef directive undefines the same macro. Hence,
the compiler
 flags an error message ‘undefined symbol ‘wait’ in function main()’.
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();
}
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>
Cont..
Examples:
 # include “stdio.h”
 # include <stdio.h>
 # include <alloc.h>
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);
}
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.
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.
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
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.
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
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])
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
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.
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.
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);
Cont..
else
{
f=ispunct(d);
if(f!=0)
printf(“n %c is a punctuation symbol”,d);
}
}
getche();
}
Enter any character : C
C is a letter in Capital case

More Related Content

Similar to Unit-IV.pptx

Chapter3
Chapter3Chapter3
Chapter3Kamran
 
C Language Programming Introduction Lecture
C Language Programming Introduction LectureC Language Programming Introduction Lecture
C Language Programming Introduction Lecturemyinstalab
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeksAashutoshChhedavi
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 

Similar to Unit-IV.pptx (20)

Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
First c program
First c programFirst c program
First c program
 
Chapter3
Chapter3Chapter3
Chapter3
 
Building Simple C Program
Building Simple  C ProgramBuilding Simple  C Program
Building Simple C Program
 
C Language Programming Introduction Lecture
C Language Programming Introduction LectureC Language Programming Introduction Lecture
C Language Programming Introduction Lecture
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

Unit-IV.pptx

  • 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
  • 12. Example (cont..) # define N 10 # define cls clrscr() # define wait() getche() # define display printf void main() { int k; cls; for(k=1;k<=N;k++) display(“ %d”,k); wait(); } OUTPUT: 1 2 3 4 5 6 7 8 9 10
  • 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>
  • 21. Cont.. Examples:  # include “stdio.h”  # include <stdio.h>  # include <alloc.h>
  • 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);
  • 33. Cont.. else { f=ispunct(d); if(f!=0) printf(“n %c is a punctuation symbol”,d); } } getche(); } Enter any character : C C is a letter in Capital case