Preprocessor Directives II-257
The
#ifdef preprocessor tests whether the identifier has defined substitute text or not. If the identifier
is defined, then #if block is compiled and executed. The compiler ignores #else block even if errors are
intentionally made. Error messages will not be displayed. If identifier is not defined, then #else block
is compiled and executed.
13. Write a program with conditional compilation statement.
Ans:
#define LINE 1
void main()
{
clrscr();
#ifdef LINE
printf("This is line number one.");
#else
printf("This is line number two.");
#endif
getche();
}
OUTPUT:
This is line number one.
Explanation: In the above program, #ifdef checks whether the LINE  identifier is defined or not.
If defined, the #if block is executed. On execution, the output of the program is “This is linenumber one”.
In case the identifier is undefined, the #else block is executed and output is “This is line number two”.
14. What are # pragma DIRECTIVE?
Ans: These #pragma directives are defined with #(hash) and these are the preprocessor directives.
These directives deal with formatting source listing and placing components in the object file generated
by the compiler.
The #pragma  directive sets/resets certain warning and errors during compilation of C program.
When a program is compiled, the compiler throws errors and warnings. The programmer should see the
errors rather than the warnings. After the removal of errors, the programmer can turn attention on warn-
ings and take further steps for sorting warnings.
15. How you set different warnings on and off?
Ans: The syntax for warning on and off is as follows:
1. #pragma warn +xxx
2. #pragma warn –xxx
The first statement turns on the warning message. The second statement sets off the warning message.
16. Write a program to set off certain errors using #pragma directives.
M12_ITL-ESL4791_02_SE_C12.indd 257 12/22/2012 5:05:48 PM
II-258 Programming Concepts
#pragma warn -aus
#pragma warn -def
#pragma warn -rvl
#pragma warn -use
int main()
{
int x=2,y,z;
printf(" y= %d",y);
}
Explanation: The above program contains the following warnings:
1. Possible use of y before definition in function.
2. z declared but never used in function main.
3. x is assigned a value, which is never used in function.
4. Function should return a value in function main.
The display of these warning messages can be made on or off by setting the pragma options. In the above
program, the four pragma options are set to off. Hence, after compilation, the above-listed lines will not
be displayed. If the four pragma options are set to on, the compiler will display these warning messages.
17. Can we undefine a predefined macro?
Ans: Yes, we can undefine a predefined macro. A macro defined with #define directives can be
undefined with #undef directive.
Syntax:
#undef identifier_macro_template substitute
It is useful when we do not want to allow the use of macros in any portion of the program.
18. Write a program to undefine a macro.
Ans:
#define wait getche()
void main()
{
int k;
#undef wait getche()
clrscr();
for(k=1;k<=5;k++)
printf("%d ",k);
wait;
}
Explanation: In the above program, wait is defined in place of getche(). In the program, #undef
directive undefines the same macro. Hence, the compiler flags an error message undefined symbol 
‘wait’ in  function  main. In case one more statement #define wait getche() is written after
Ans:
M12_ITL-ESL4791_02_SE_C12.indd 258 12/22/2012 5:05:48 PM
Preprocessor Directives II-259
# undef wait getche()”, then the compiler does not show any error and the output displayed on the
screen would be “1 2 3 4 5”.
19. What is an assert() macro?
Ans: An assert macro is defined assert.h header file. This macro tests the value of an expression.
If the expression contains false value, assert displays error message and executes the function abort() to
abort the program execution.
20. Write a program on an assert() macro.
Ans:
#include <assert.h>
void main()
{
int x=4;
clrscr();
assert(x!=4);
printf("%d",x);
}
OUTPUT:
Assertion failed: x!=4, file AMITPOINTERE.C, line 7
Abnormal program termination
Explanation: In this program, the value of x is 4. The assert() macro checks the value of x. If the
condition is false, the macro executes abort() and the program is terminated.
Multiple-choice Questions
1. Consider the statement #define PI 3.14, it means .
(a) Every occurrence of PI (identifier) is replaced with 3.14 (substitute value)
(b) Occurrence of PI only in expressions is replaced with 3.14
(c) None of the above
2. A macro defined with # define directives can be undefined with .
(a) #undef directive (b) #ifndef (c) #!def
3. The file name is included in the double quotations marks indicates that .
(a) The search for the file is to be made in the current directory and in the standard directories
(b) The search for the file is to be made in the current directory
(c) The search for the file is to be made in the entire system
4. When the file name is included without double quotation marks and when program is executed, the
message appears on the screen will be .
(a) The search for file is made only in the standard directories
(b) The search for file is made only in the current directory
(c) The search for the file is to be made in the entire system
(d) Bad file name format in include directory
M12_ITL-ESL4791_02_SE_C12.indd 259 12/22/2012 5:05:48 PM
II-260 Programming Concepts
5. Generally, the standard directories for header file and library files are .
(a) include and lib (b) turboc2 and include (c) tc2 and lib
6. The standard directory called ‘include’ contains .
(a) Header files (b) Library file (c) Program file
7. The standard directory called ‘lib’ contains .
(a) Header files (b) Library file (c) Program file
8. The conditional compilation directives allow the programmer to .
(a) Compile a part of program (b) Compile entire program
(c) Compile program excluding header files
9. In the statement #ifdef, #else, #endif, the compiler ignores #else block .
(a) When macro is not defined
(b) When macro is defined
(c) None of the above
10. Which of the following is not a preprocessor directive?
(a) #undef (b) #define (c) #2def
11. Which of the following preprocessor directive works exactly opposite to #ifdef?
(a) #undef (b) #ifndef (c) #nodef
12. Which of the following preprocessor directive displays user-defined error message?
(a) #undef (b) #error (c) #errordef
13. What is the output of the following program?
#define SQUARE X*X*X
void main()
{
int X=10;
printf("%d",SQUARE);
getche();
}
(a) 100 (b) 1000 (c) 10
#define PI 3.14
void main()
{
float x;
clrscr();
for(x=PI*2;x<=7.28;x++)
printf("%g",x);
getche();
}
14. What is the output of the following program?
M12_ITL-ESL4791_02_SE_C12.indd 260 12/22/2012 5:05:48 PM
Preprocessor Directives II-261
(a) 7.28 (b) 6.28
(c) 6.28 (d) 7.28
15. The program typed in the editor is the to the preprocessor.
(a) Source code (b) Object code (c) ASCII code
16. The preprocessor passes the source code to the C .
(a) Compiler (b) Assembler (c) Interpreter
17. The preprocessor directives are always included at the .
(a) Beginning of the program (b) Run time (c) Compile time
18. The preprocessor begins with the symbol .
(a) // (b) # (c) $
19. A macro defined with directives can be undefined with #undef directive.
(a) #define (b) #include (c) #ifdef
20. In operation, the macro argument is converted to string.
(a) String concatenation (b) Stringizing operation (c) String coping
21. The loads specified file in the current program.
(a) #include (b) #define (c) #ifndef
22. The is used to display user-defined message during compilation of the program.
(a) #error (b) #pragma (c) #stderr
23. Inline directive reports the compiler that the source code has in line statements.
(a) bsm code (b) asm code (c) C++ code
24. What will be the value of y after the execution of the following program?
#include <stdio.h>
#include <conio.h>
#define plus(x) x;
#define minus(x) --x+ plus(x);
void main()
{
int x=8,y;
clrscr();
y=minus(x)
printf(" y = %d",y);
}
(a) y = 14 (b) y = 15
(c) y = 13 (d) None of the above
25. 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++;
M12_ITL-ESL4791_02_SE_C12.indd 261 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