Preprocessor Directives
1. What is a preprocessor directive?
Ans: One of the most important features of ‘C’ language is to offer preprocessor directives. They are
not the program statements but directives to the preprocessor. The preprocessor directives are included
at the beginning of the program before the main(). They can be placed anywhere but quite often it is
included at the beginning before the main() or any particular function. They begin with the symbol #
(hash) to be placed at the first column.
2. How to define preprocessor macro?
Ans: The preprocessor macro is defined as follows.
#define identifier substitute
The preprocessor replaces any occurrence of identifier in the program by substitute.
For example,
#define PI 3.14
This statement defines PI as macro template 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 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. It is optional to the programmer. To increase readability, the programmer should provide
space. The macro definition should not be terminated with semicolon i.e. preprocessor directives are not
terminated by semicolons.
3. Replace PI as 3.14 in the program and find the area of a circle.
Ans:
12
#define PI 3.14
void main()
{
float r,area;
clrscr();
M12_ITL-ESL4791_02_SE_C12.indd 252 12/22/2012 5:05:48 PM
Preprocessor Directives II-253
Explanation: In the above program, PI replaces with 3.14 during the program execution. In the
program, instead of writing the value of PI as 3.14, we define directly the value of PI as 3.14. The term
PI is replaced with 3.14 in the program. PI macro is used for calculating the area of a circle.
4. What is the  #include directive? How does it help the programmer?
Ans: The #include directive loads specified file in the current program. The included programmers
file executes along with the current program. The macros and functions of loaded file can be included
in the current program. Both the included file and the current program are complied.
5. What is the syntax of #include directive? Elaborate it with examples.
Ans: Syntax of #include directive is as follows:
(a) #include "filename"
or
(b) #include <filename>
where # is a symbol used with directives.
(a) The file name is included in the double quotation marks indicating that the search for the file
is made in the current directory and in the standard directories.
For example, #include "stdio.h"
(b) When the filename is enclosed within angle brackets without double quotation marks, the
search for file is made only in the standard directories.
For examples, #include <stdio.h>
#include "udf.h"
6. Write a program on include directive.
Ans:
printf(" Enter radius of circle in cms :-");
scanf("%f",&r);
area=PI*r*r;
printf("Area of a Circle = %.2f cm^2",area);
getche();
}
OUTPUT:
Enter radius of circle in centimetres: 7
Area of a circle =153.86 cm^2
#include <stdio.h>
#include "abc.c"
#include <assert.h>
M12_ITL-ESL4791_02_SE_C12.indd 253 12/22/2012 5:05:48 PM
II-254 Programming Concepts
Explanation: In this program, the abc.c
file is included. Its contents are simply int a=4; it is a user-
defined function file. It is complied before the main program. The complete program along with the
included file gets executed. The output of the program is as shown above.
7. What is the difference between functions and preprocessor directives?
Ans: The preprocessor directives are normally included at the beginning of the program or before the
main() function in C. They alter the source code through macro expansion, produce precompiled files
and finally pass it for compilation.
However, function is block of statement(s) that is to be invoked by the programmer from one function
for carrying out task repeatedly if needed. The functions are called methods or subprograms. Calling
subprogram from one function and executing the same subprogram requires more time than execution
of preprocessor directives. Hence, preprocessor directive execution is fast.
8. What is the use of stringizing operator?
Ans: The stringizing operator converts macros arguments into strings. The stringizing operator #
carries the operation. It is placed before the argument.
For example,
void main()
{
int x=4;
clrscr();
assert(x==4);
printf("%d",x);
printf("%d",a);
}
OUTPUT:
44
#define say(m) printf(#m)
void main()
{
clrscr();
say(Hello);
getche();
}
OUTPUT:
Hello
M12_ITL-ESL4791_02_SE_C12.indd 254 12/22/2012 5:05:48 PM
Preprocessor Directives II-255
Explanation: In the above program, after conversion, the statement
say(Hello)  is treated as
printf("Hello"). It is not essential to enclose the text with quotation marks in the stringizing
operator. If # is removed from macro definition, the user has to enclose the text in double quotes.
9. Write a program to double and triple a given number using stringizing operator.
Ans:
#define DOUBLE(x) printf("Double of "#x" = %d ",x*2)
#define TRIPLE(x) printf("Triple of "#x" = %d ",x*3)
void main()
{
int m;
clrscr();
printf("Enter a number :");
scanf ("%d",&m);
DOUBLE(m);
TRIPLE(m);
getche();
}
OUTPUT:
Enter a number: 5
Double of m = 10
Triple of m = 15
Explanation: In the above program, the value of mis passed to DOUBLE() and TRIPLE() macros that
is assigned to x. #x prints the name of the variable passed through the macros.
10. List predefined macros according to ANSI standard together with their uses.
Ans: Every predefined macro name is defined with two underscores as prefix and suffix. These
macros are useful for finding system information such as date, time, file name and line number.
Predefined macro functions together with their meaning are as follows:
_ _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
_ _STDC_ _ In ANSI ‘C’, the value returned will be non-zero.
M12_ITL-ESL4791_02_SE_C12.indd 255 12/22/2012 5:05:48 PM
II-256 Programming Concepts
11. Write a program on predefined macros.
Ans:
#include <stddef.h>
void main()
{
clrscr();
printf(" DATE : %s",__DATE__);
printf(" TIME : %s",__TIME__);
printf(" FILE NAME : %s",__FILE__);
printf(" LINE NO. : %d",__LINE__);
}
OUTPUT:
DATE : Jul 04 2011
TIME : 20:08:12
FILE NAME : POINTERE.C
LINE NO. : 9
Explanation: In the above program, five macros are used in the printf() statements. On its
execution, the output of the program is displayed as shown above. The program displays system date, 
time,  program file  name  and  total  lines  in the program. The STDC  indicates whether or not the
version of C compiler follows ANSI ‘C’ standard . Here, the result is one. Hence, the compiler follows
ANSI C standards. The programmer should set ANSI keyword on option by selecting option menu of the
editorcompiler-source – ANSI keywords only on.
12. Elaborate on conditional compilation.
Ans: Quite often, one can use conditional compilation directives in the programs. The most frequently
used conditional compilation directives are #ifdef, #else, #endif etc. These directives allow the program-
mer to include the portions of the codes based on the conditions. The compiler compiles selected portion
of the source codes based on the conditions. The syntax of the #ifdef directive is as follows:
Syntax:
#ifdef identifier
{
statement1;
statement2;
}
#else
{
statement3;
statement4;
}
#endif
M12_ITL-ESL4791_02_SE_C12.indd 256 12/22/2012 5:05:48 PM
..................Content has been hidden....................

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