II-262 Programming Concepts
#define plus(x) P
void main()
{
int x=2,y;
clrscr();
y=plus(x)
printf(" x = %d y = %d",x,y);
}
(a) x = 3 y = 2
(b) x = 3 y = 3
(c) x = 2 y = 2
(d) None of the above
26. In the following example, whether macro is treated as .
#include <stdio.h>
#include <conio.h>
#define S "This Book Teaches C"
void main()
{
printf(" %s",S);
}
(a) Macro as well as array (b) Only macro
(c) Only array (d) None of the above
27. What will be the output after execution of the following program?
#include <stdio.h>
#include <conio.h>
#define S "This Book Teaches C"
void main()
{
clrscr();
printf(" %c",*(S+3));
}
(a) s (b) h
(c) i (d) T
28. What will be the value of variable z after the execution of the following program?
#include <stdio.h>
#include <conio.h>
#define ROW 2
#define COL 3
M12_ITL-ESL4791_02_SE_C12.indd 262 12/22/2012 5:05:48 PM
Preprocessor Directives II-263
int a[ROW][COL]={8,6,4,2,0,-2};
void main()
{
int x,y,z=0;
clrscr();
for(x=0;x<ROW;x++)
for(y=0;y<COL;y++)
if(a[x][y]>z)
z=a[x][y];
printf(" z = %d",z);
}
(a) z = 8 (b) z = −2
(c) z = 0 (d) z = 2
29. What will be the value of variables k and m after the execution of the following program?
#include <stdio.h>
#include <conio.h>
#define product(k) k*k
void main()
{
int k=3,m;
m=product(k++);
clrscr();
printf(" k=%d m=%d",k,m);
}
(a) k = 5 m = 9
(b) k = 4 m = 16
(c) k = 5 m = 25
(d) k = 4 m = 9
30. What will following program the display?
#include <stdio.h>
#include <conio.h>
#define P &x
void main()
{
int x=2;
clrscr();
printf(" %u",P);
}
M12_ITL-ESL4791_02_SE_C12.indd 263 12/22/2012 5:05:49 PM
II-264 Programming Concepts
(a) Address (b) Value
(c) Error message (d) None of the above
31. What is the output of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int x=2,*p=5;
p=&x;
#define P &p
clrscr();
printf(" %d",**P);
}
(a) 2 (b) 5
(c) 65500 (d) None of the above
Answers
1. (a) 2. (a) 3. (a) 4. (d) 5. (a) 6. (a) 7. (b) 8. (a) 9. (b) 10. (c)
11. (b) 12. (b) 13. (b) 14. (b) 15. (a) 16. (a) 17. (a) 18. (b) 19. (a) 20. (b)
21. (a) 22. (a) 23. (b) 24. (d) 25. (a) 26 (a) 27. (a) 28. (a) 29. (a) 30. (a)
31. (a)
True or False
1. The pre-processor is executed before the actual compilation of program code begins.
2. The program typed in the editor is source code for the pre-processor.
3. The #define defines the macro templates.
4. The macro definition must be terminated by semi-colon.
5. The define is a keyword.
6. The #undef undefines the macro.
7. The #include loads the specified file.
8. #include "stdio.h" the compiler searches the file in entire system.
9. #include <stdio.h> the compiler searches the file in standard directory.
10. Conditional compilation means few statements can be skipped from compiler.
11. The #ifdef and #ifndef work exactly the same way.
12. The #error flags are user-defined messages.
M12_ITL-ESL4791_02_SE_C12.indd 264 12/22/2012 5:05:49 PM
Preprocessor Directives II-265
13. The #pragma sets off/on warning and error messages.
14. The #ninclude closes the file loaded by #include.
Answers
1. True 2. True 3. True 4. False 5. False 6. True 7. True 8. False 9. True 10. True
11. False 12. True 13. True 14. False
Additional Questions
What is/are the output/s of the following programs?
1.
#include <stdio.h>
#include <conio.h>
#define PI 3.14
void main()
{
float r=2.2,area;
area=PI*r*r;
clrscr();
printf("Area of a Circle = %.2f cm2",area);
}
OUTPUT:
Area of Circle =15.20 cm2
2.
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include <conio.h>
#define N 5
#define say printf
void main()
{
int k;
clrscr();
for(k=1;k<=N;k++)
say (" %d ",k);
}
OUTPUT:
1 2 3 4 5
M12_ITL-ESL4791_02_SE_C12.indd 265 12/22/2012 5:05:49 PM
II-266 Programming Concepts
3.
#include <stdio.h>
#include <conio.h>
#define DOUBLE(a) a*2
void main()
{
int a=1;
clrscr();
for(;a<=5;a++)
printf(" %d",DOUBLE(a));
}
OUTPUT:
2 4 6 8 10
4.
#include <stdio.h>
#include <conio.h>
#define say(m) printf(#m)
void main()
{
clrscr();
say(C is portable );
}
OUTPUT:
C is portable
5.
#include <stdio.h>
#include <conio.h>
#define MAX(x,y) if(x>y) c=x; else c=y;
void main()
{
int x=3,y=5,c;
clrscr();
MAX(x,y);
printf(" Largest of two numbers = %d",c);
}
OUTPUT:
Largest of two numbers = 5
M12_ITL-ESL4791_02_SE_C12.indd 266 12/22/2012 5:05:49 PM
..................Content has been hidden....................

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