1. What will be the output?
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf("%d %d\n", x, y);
return 0;
}
A. It compiles
B. Compiles with an warning
C. Not compile
D. Compiles and print nothing
(NB:The code won't compile since declaration of t cannot occur within parenthesis. )
............................................................................................
2.In which stage the following code
#include<stdio.h>
gets replaced by the contents of the file stdio.h
A. During editing B. During linking C. During execution D. During preprocessing
(NB:The preprocessor replaces the line #include <stdio.h> with the system header file
of that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive. )
..............................................................................................
3. What will be the output of the program?
#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);
int main()
{
int i=10, j=5, k=0;
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
}
A. 12, 6, 12 , B. 11, 5, 11 , C. 11, 5, Garbage , D. 12, 6, Garbage
(NB:The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.
Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.
Step 2: k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12
Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.
In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.
Hence the output of the program is 12, 6, 12)
............................................................................................................
#include<stdio.h>
#define PRINT(int) printf("int=%d, ", int);
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
A. int=2, int=3, int=4 B. int=2, int=2, int=2
C. int=3, int=3, int=3 D. int=4, int=4, int=4
(NB:The macro PRINT(int) print("%d,", int); prints the given variable value in an integer format.
Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.
Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.
Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.
Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.
Hence the output of the program is int=2, int=3, int=4.)
.............................................................................................................
#include<stdio.h>
#define FUN(i, j) i##j
int main()
{
int va1=10;
int va12=20;
printf("%d\n", FUN(va1, 2));
return 0;
}
A. 10 , B. 20, C. 1020 , D. 12
(NB:The following program will make you understand about ## (macro concatenation) operator clearly.)
..........................................................................................................
#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)
int main()
{
int x;
x = MAX(3+2, 2+7);
printf("%d\n", x);
return 0;
}
A. 8, B. 9 , C. 6 , D. 5
(NB:The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.
Step 1 : int x; The variable x is declared as an integer type.
Step 2 : x = MAX(3+2, 2+7); becomes,
=> x = (3+2 > 2+7 ? 3+2 : 2+7)
=> x = (5 > 9 ? 5 : 9)
=> x = 9
Step 3 : printf("%d\n", x); It prints the value of variable x.
Hence the output of the program is 9.)
................................................................................................................
#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
int x=3, y=4, z;
z = MIN(x+y/2, y-1);
if(z > 0)
printf("%d\n", z);
return 0;
}
A. 3 B. 4 C. 0 D. No output
(NB:The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.
Step 1: int x=3, y=4, z; The variable x, y, z are declared as an integer type and the variable x, y are initialized to value 3, 4 respectively.
Step 2: z = MIN(x+y/2, y-1); becomes,
=> z = (x+y/2 < y-1)? x+y/2 : y - 1;
=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;
=> z = (3+2 < 4-1)? 3+2 : 4 - 1;
=> z = (5 < 3)? 5 : 3;
The macro return the number 3 and it is stored in the variable z.
Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.
Step 4: printf("%d\n", z);. It prints the value of variable z. Hence the output of the program is 3)
........................................................................................................
#include<stdio.h>
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)
int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d\n", x);
return 0;
}
A. 5 , B. 9, C. 10 ,D. 3+7
(NB:The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.
Step 1: int x; The variable x is declared as an integer type.
Step 2: x = MAX(3+2, 2+7, 3+7); becomes,
=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 >9 ? (10): (10) )
=> x = 10
Step 3: printf("%d\n", x); It prints the value of 'x'.
Hence the output of the program is "10".)
No comments:
Post a Comment