Preprocessor Directives 371
printf ( “ Small Case");
}
else
{
f=isdigit(d);
if(f!=0)
printf (" n %c is a digit",d);
else
{
f=ispunct(d);
if(f!=0)
printf (" n % c is a punctuation sym bol",d);
}
}
)
getcheO;
OUTPUT:
Enter any character: C
C is a letter in Capital case
Explanation In the above program a character is entered through the keyboard. The macro i salpha
() checks whether it is a digit or a letter. It returns true or false value to variable d. The i f condition
checks the value of variable d. If the entered character is a letter the i f block is executed and again the
macro i supper () checks whether the character is capital or small. Thus appropriate messages are
displayed.
If theentered character isnot aletter else block of firstif statement is executed. The macro is d igit
() checks whether the character is digit or any other symbol. If it is digit i f block is executed otherwise
else block is executed. In the else block macro ispunc t () checks whether the entered character is
punctuation symbol. If so the message is displayed.
SU MM AR Y
In this chapter you have studied one of the most useful feature of the C language i.e. the preprocessor
directive. It supports the programmer to write portable programs, which can be executed, on different
types of systems. After having gone through this chapter and on execution of programs, you also have
the knowledge and uses of #def ine # #undef, # include, #lin e, token pasting and stringizing operator,
conditional compilation through illustrated examples. You too, have learnt how to display programmer's
own error messages using te rro r d irectiv e and making various warnings on/off displayed by
compiler using #pragma directive. You are also aware of the pre-defined macros and their uses.
EXE RC ISES
A) Answ er the following questions to the p o in t
1) What is a preprocessor directive?
2) What is the # inc lude directive? H ow does it help the programmer?
372 Programming and Data Structures
3) Distinguish between function and preprocessor directive?
4) What do you mean by token pasting and stringizing operators?
5) List predefined macros according to ANSI standard together with their uses.
6) Elaborate conditional compilation.
7) What are # PRAGMA DIRECTIVE? How do you set them on and off?
8) Can we undefine a pre-defined macro?
B) Answ er the following by selecting the appropriate option.
1) What will be the value of y after execution of the following program?
# include <stdio.h>
# include <canio.h>
# define plus (x) x;
# define minus (x) - -x+ plus (x );
void main()
{
int x=8,y;
clrscr ();
y=aninus(x)
prin tf ("n y = %d",y);
}
a) y=14 b) y=15
c) y=13 d) none of the above
2) What will be the values of variables x and y after execution of the following program?
# include <stdio.h>
# include <conio.h>
# define P x++;
# define plus (x) P
void main()
{
int x=2,y;
clrscr();
y=plus (x)
prin tf ("nx = %d y = % d",x,y);
}
a) x=3 y=2
c) x=2 y=2
b) x=3 y=3
d) none of the above
3) In the following example whether macro is treated as
#include <stdio.h>
#include cconio. h>
# define S "This Book Teaches C"
void main()
{
printf ("n %s",S);
}
a) macro as well as array
c) only array
b) only macro
d) none of the above
Preprocessor Directives 373
4) What will be the output after execution of the following program?
#include <stdio.h>
#include <conio.h>
# define S "This Book Teaches C"
void xnainO
{
clrscr();
printf (" %c", * (S+3) ) ;
}
a) s b) h c) i d) T
5) What will be the value of variable z after execution of the following program?
#include <stdio.h>
#include <canio.h>
# define ROW 2
# define COL 3
int a [ROW][COL]={8,6,4,2,0,-2};
void main()
{
int x,y, zmO;
clrscr ();
for (x=0;x<RCW;x++)
for (yaO;y<OOL;y++)
i f (a[x] [y] >z)
z=a [x] [y ];
printf (" z = %d",z);
a) z=8 b) z=-2 c) z=0 d) z=2
6) What will be the value of variable k and m after execution of the following program?
#include <stdio.h>
#include ccomio. h>
# define product (k) k*k
void main ()
{
int k=3,m;
nfc*product (k++);
clrscr ();
printf ("t k*%d xft=%d#y#k/m);
}
a) k=5 m=9
c) k=5 m=25
b) k=4 m=16
d) k=4 m=9
374 Programming and Data Structures
7) The following program will display
#include <stdio.h>
#include <canio.h>
# define P &x
void mainO
{
int x=2;
clrscrO ;
printf (nt %u", P );
}
a) address
c) error message
8) The following program will display the output
#include <stdio.h>
#include <conio.h>
void mainO
{
int x=2, *p=5;
p=&x;
# define P &p
clrsc rO ;
prin tf (*t %d",**P);
}
a) 2
c) 65500
C] Attem pt the following program s.
1) Write a program to check whether the entered character is digit or letter. Display appropriate
messages. Use macros defined in "ctype.h" header file.
2) Write a program to calculate average of five numbers. Define macro for number five.
3) Write a program to define macros for opening and closing curly braces with BEGIN and END
respectively. Use these macros in C program in place of opening and closing curly braces.
4) Write a program to undefine a macro defined in # define directive. Observe the result.
5) Write a program to explain conditional compilation.
6) Write a program to display user-defined error message using #error directive.
7) Write a program to define macro for function main (). Call function using the macro.
8) Write a program to find the smallest and the largest out of three numbers using macros with
arguments.
9) Write programs to use macros as an array and pointer.
b) 5
d) none of the above
b) value
d) none of the above
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.185