O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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("\n 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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required