0% found this document useful (0 votes)
9 views45 pages

C Program Output with Preprocessor Directives

The document contains a series of C programming questions and answers related to preprocessor directives. Each question presents a code snippet and asks for the expected output, followed by the correct answer and an explanation. The topics covered include macro definitions, conditional compilation, and the effects of redefining macros.

Uploaded by

hegoji5670
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views45 pages

C Program Output with Preprocessor Directives

The document contains a series of C programming questions and answers related to preprocessor directives. Each question presents a code snippet and asks for the expected output, followed by the correct answer and an explanation. The topics covered include macro definitions, conditional compilation, and the effects of redefining macros.

Uploaded by

hegoji5670
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Preprocessor Directives

What will be the output of the C program?


#include <stdio.h>
#define int char
main()
{
int i = 5;
printf("%d", sizeof(i));
return 0;
}
A. 2
B. 4
C. Compilation error
D. 1
ANSWER: D

Explanation
Since the #define replaces the string int by the macro char.
What will be the output of the C program?
#include <stdio.h>
#define x 2
int main()
{
int i;
i = x * x * x;
printf("%d", i);
return 0;
}
A. 8
B. x is not declared
C. No output
D. Garbage value
ANSWER: A

Explanation
Since we replace x = 8 in #define.

2 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
#define square(x) x*x
int main()
{
int i;
i = 64 / square(4);
printf("%d", i);
return 0;
}
A. 16
B. Compilation error
C.4
D. 64
ANSWER: D

Explanation
Operators enjoys priority / is given more priority over *. In this program the execution takes
place in this format.
64 / square(4)
64 / 4*4
16 * 4
64

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 3


What will be the output of the C program?
#include <stdio.h>
#define i 10
int main()
{
#define i 20
printf("%d", i);
return 0;
}
A. Compilation error
B. 20
C. 10
D. Runtime error
ANSWER: B

Explanation
The preprocessor directives can be redefined anywhere in the program. So the most recently
assigned value (#define i 20) will be taken.

4 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
#define clrscr() 50
int main()
{
clrscr();
printf("%d", clrscr());
return 0;
}
A. Compilation error
B. Runtime error
C. 50
D. none of the mentioned
ANSWER: C

Explanation
Preprocessor in any programming language executes as a separate pass before the execution of
the compiler. So textual replacement of clrscr() to 50 occurs with no errors.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 5


What will be the output of the C program?
#include <stdio.h>
#define x 10;
int main()
{
printf("%d",x);
return 0;
}
A. Garbage value
B. Runtime error
C.10
D. Compilation error
ANSWER: D

Explanation
Preprocessor should not terminate with semicolon.

6 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
#define loop while(1)
int main()
{
loop;
printf("preprocessor-aptitude");
return 0;
}
A. program never ends
B. Compilation error
C. preprocessor-aptitude
D. none of the mentioned
ANSWER: A

Explanation
Program never ends and no output will be printed as infinite execution starts for loop; itself.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 7


What will be the output of the C program?
#include <stdio.h>
# define x - -5
int main()
{
printf("%d", x);
return 0;
}
A. Compilation error
B. -
C. -5
D. 5
ANSWER: D

8 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
# define x --5
int main()
{
printf("%d", x);
return 0;
}
A. -
B. Compilation error
C. 5
D. -5
ANSWER: B

Explanation
We cannot decrement or increment the value directly. Thus compilation error occurs.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 9


What will be the output of the C program?
#include <stdio.h>
#define sqr(x) ++x * ++x
int main()
{
int a = 3, z;
z = ++a * ++a;
a -= 3;
printf("%d %d", sqr(a), z);
return 0;
}
A. 25 16
B. 16 16
C. 16 25
D. 25 25
ANSWER: C

Explanation
Line 6: a = 3 .
Line 7: z = 4 * ++a.
Line 7: z = 4 * 5.
Line 7: z = 5 * 5.
Line 7: z = 25.
Line 8: a = 5 - 3.
Line 8: a = 2.
Line 9: here line 2 will execute .
Line 2: #define sqr(2) 3 * 4.
Line 2: #define sqr(2) 4 * 4.
Line 2: #define sqr(2) 16.
Now 16 25will be printed as per our printf() function.

10 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
#define x 1+2
int main()
{
int i;
i = x * x * x;
printf("%d", i);
}
A. 27
B. 13
C. 7
D. None of the mentioned
ANSWER: C

Explanation
i = x * x * x;
i = 1+2 * x * x;
i = 3 * x * x;
i = 3 * 1+2 * x;
i = 3 + 2 * x;
i = 5 * x;
i = 5 * 1 + 2;
i = 5 + 2;
i = 7;
Thus C compiler outputs i = 7

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 11


What will be the output of the C program?
#include <stdio.h>
#define a =
int main()
{
int num a 6;
printf("%d", num);
return 0;
}
A. 6
B. Compilation error
C. Garbage value
D. Runtime error
ANSWER: A

Explanation
= is defined in proprocessor with the name of a. Thus no problem with a normal compilation
flow.

12 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
#define fun(x,y) x*y
int main()
{
int x = 2, y = 1;
printf("%d", fun(x + 2, y - 1));
return 0;
}
A. 4
B. 2
C. 0
D. 3
ANSWER: D

Explanation
In printf we called the function fun(x + 2, y - 1);.
Now printf looks like this fun(2 + 2, 1 - 1). and control goes to #define preprocessor directive.
fun(2 + 2, 1 - 1) 2 + 2 * 1 - 1
fun(2 + 2, 1 - 1) 2 + 2 - 1
fun(2 + 2, 1 - 1) 4 - 1
fun(2 + 2, 1 - 1) 3
Thus it returns 3 to the printf and outputted.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 13


What will be the output of the C program?
#include <stdio.h>
#define puts "%s C preprocessor"
int main()
{
printf(puts, puts);
return 0;
}
A. Runtime error
B. Compilation error
C. %s C preprocessor %s C preprocessor
D. %s C preprocessor C pre-processor
ANSWER: D

Explanation
printf(puts,puts); will call the preprocessor macro # define puts.
printf("%s C preprocessor","%s C preprocessor");
printf(%s C preprocessor C preprocessor);

14 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program ?
#include <stdio.h>
#include<string.h>
#define MACRO(num) ++num
int main()
{
char *ptr = "preprocessor";
int num = strlen(ptr);
printf("%s ", MACRO(ptr));
printf("%d", MACRO(num));
return 0;
}
A. preprocessor 12
B. preprocessor 13
C. reprocessor 13
D. reprocessor 12
ANSWER: C

Explanation
int num = strlen(ptr);
int num = 12;
printf("%s", MACRO(preprocessor));
printf("%s", reprocessor);
printf("%d", MACRO(num));
printf("%d", 13);
Thus output is reprocessor 13

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 15


What will be the output of the following C code?
#include <stdio.h>
#define max 100
int main()
{
#ifdef max
printf("hello");
return 0;
}
A. 100
B. hello
C. "hello"
D. error
ANSWER: D

Explanation: The code shown above results in an error. This is because the preprocessor #endif
is missing in the above code. If the identifier is defined, then the statements following #ifdef are
executed till #endif is encountered.

16 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


_____ is the preprocessor directive which is used to end the scope of #ifdef.
A. #elif
B. #ifndef
C. #endif
D. #if
ANSWER: C

Explanation: The #ifdef preprocessor directive is used to check if a particular identifier is


currently defined or not. If the particular identifier is defined, the statements following this
preprocessor directive are executed till another preprocessor directive #endif is encountered.
#endif is used to end the scope of #ifdef.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 17


What will be the output of the following C code?
#include <stdio.h>
int main()
{
#ifndef max
printf("hello");
#endif
printf("hi");
return 0;
}
A. hello
B. hellohi
C. error
D. hi
ANSWER: B

Explanation: The code shown above illustrates the preprocessor directive #ifndef. If the
identifier checked is not defined, then the statements following #ifndef are executed. Here,
since the identifier max is not defined, the statement after #ifndef is executed. Hence the output
is: hellohi

18 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define rec 557
int main()
{
#ifndef rec
printf("yes");
#endif
printf("no");
return 0;
}
A. error
B. yes
C. no
D. yesno
ANSWER: C

Explanation: The output to the above code will be no. Since we have used the preprocessor
directive, #ifndef and defined the identifier rec, “yes” is not printed. Hence only “no” is printed
as output.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 19


The preprocessor directive which checks whether a constant expression results in a zero or
non-zero value _____.
A. #if
B. #ifdef
C. #undef
D. #ifndef
ANSWER: A

Explanation: #if checks whether a constant expression results in zero or not. If the expression
is a non-zero value, then the statements between #if and #endif will be executed. If the constant
expression results in a zero value, the statements between #if and #endif will not be executed.

20 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?

#include <stdio.h>
#define max 100
int main()
{
#if(max%10)
printf("REC");
#endif
printf("RIT");
return 0;
}

A. error
B. REC
C. RIT
D. RECRIT
ANSWER: C

Explanation: The code shown above is an illustration of the preprocessor directive #if. The
value of the defined identifier max is 100. On checking the condition (100%10==0), which is
false, the statements after #if are not executed till #endif is encountered. Hence the output is
RIT.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 21


The preprocessor directive which is used to remove the definition of an identifier which was
previously defined with #define?
A. #ifdef
B. #undef
C. #ifndef
D. #def
ANSWER: B

Explanation: #undef is used to remove the definition of any identifier which had been
previously defined in the code with #define.

22 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define hello 10
int main()
{
printf("%d", hello);
#undef hello
printf("%d", hello);
return 0;
}
A. 10
B. hello
C. error
D. 1010
ANSWER: C

Explanation: Error: hello undeclared. An error is thrown when the code shown above is
executed because before the second call to the printf function, the macro hello was undefined
using #undef.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 23


What will be the output of the following C code?
#include <stdio.h>
#define a 2
int main()
{
int r;
#define a 5
r = a * 2;
printf("%d", r);
return 0;
}
A. 10
B. 4
C. 2
D. 5
ANSWER: A

Explanation: The macro ‘a’ is redefined in the code shown above. Hence the value of a, which is
initially 2, is redefined to 5. The value stored in ‘r’ is the result of the expression (a*2), which is
equal to 10. Hence the output of this code will be 10.

24 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code if the value of ‘p’ is 10 and that of ‘q’ is 15?
#include <stdio.h>
int main()
{
int p, q;
printf("Enter two numbers\n");
scanf("%d", &p);
scanf("%d", &q);
#if(4<2)
printf("%d",p);
#elif(2>-1)
printf("%d", q);
#else
printf("bye");
#endif
return 0;
}
A. 10
B. 15
C. bye
D. error
ANSWER: B

Explanation: The output of the code shown above is 15. The values given for ‘p’ and ‘q’ are 10
and 15 respectively. Since the condition given for #elif is true, the value stored in ‘q’ will be
printed, that is 15.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 25


What will be the output of the following C code?
#include <stdio.h>
#define rec 10
int main()
{
#ifdef rec
#define rec 20
#endif
printf("%d", rec);
return 0;
}
A. 10
B. 20
C. Error
D. 1020
ANSWER: B

Explanation: In the code shown above, if the identifier rec is defined, then its value is redefined
and changed to 20. It is then printed. Hence the output is 20.

26 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define hello
int main()
{
#ifdef hello
#define hi 4
#else
#define hi 5
#endif
printf("%d", hi);
return 0;
}
A. 4
B. 5
C. 45
D. error
ANSWER: A

Explanation: The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier
hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is
4. If hello was not defined, then the value of hi would be 5.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 27


The purpose of the preprocessor directive #error is that _____.
A. It rectifies any error present in the code
B. It rectifies only the first error which occurs in the code
C. It causes the preprocessor to report a fatal error
D. It causes the preprocessor to ignore an error
ANSWER: C

Explanation: #error is a preprocessor directive which causes the preprocessor to report a fatal
error. The tokens which form the rest of the line after #error are used as the error message.

28 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define max 20
int main()
{
#ifndef max
#define min 10
#else
#define min 30
#endif
printf("%d", min);
return 0;
}
A. 10
B. 20
C. 30
D. error
ANSWER: C

Explanation: The output of the code shown above is 30. Since the identifier max is defined (the
condition of #ifndef is true), hence another identifier, that is, min is defined and its value is
equal to 30.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 29


What will be the output of the following C code?
#include <stdio.h>
#define hello 10
main()
{
#ifdef hello
#undef hello
#define hello 100
#else
#define hello 200
#endif
printf("%d", hello);
}
A. Error
B. 10
C. 100
D. 200
ANSWER: C

Explanation: The output of the code shown above is 100. If the identifier hello is defined, then
it is undefined and redefined. It’s new value is 100. If the identifier was not defined, it would be
defined with a value of 200.

30 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define rec 10
int main()
{
if (rec == 100)
printf("good");
else
{
printf("bad");
rec = 100;
}
printf("%d", rec);
return 0;
}
A. 100
B. bad
C. 10
D. error
ANSWER: D

Explanation: The above code will result in an error because a macro cannot be defined using a
simple assignment operator. In the code shown above, the value of rec is changed to 100 using
the assignment operator. This causes an error.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 31


What will be the output of the following C code?
#include <stdio.h>
void f()
{
#define rec 100
printf("%d", rec);
}
int main()
{
#define rec 99;
f();
printf("%d",rec);
return 0;
}
A. error
B. 100
C. 99
D. 10099
ANSWER: A

Explanation: Macro definition is global even if it is appears within the function scope. In this
case the macro rec is defined in the function f(). Hence it results in a compile time error.

32 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C code?
#include <stdio.h>
#define INDIA 1
#define US 2
#define CC US
main()
{
#if CC==INDIA
printf("Rupee");
#elif CC==US
printf("Dollar");
#else
printf("Euro");
#endif
return 0;
}
A. Euro
B. Rupee
C. Dollar
D. Error
ANSWER: C

Explanation: The output of the code shown above is Dollar. Since the macro CC is defined as US
at the beginning of the code, it satisfies the condition #elif(CC==US). Hence “Dollar” is printed.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 33


What will be the output of the following C code?
#define sqr(x) x*x
int main()
{
int a1;
a1 = 25 / sqr(5);
printf("%d", a1);
return 0;
}
A. 25
B. 1
C. 5
D. error
ANSWER: A

Explanation: The output of the code shown above is 25. This is because the arithmetic statement
takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x)
(x*x), the output would have been 1.

34 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


Which of the following is not a preprocessor directive?
A. #error
B. #pragma
C. #if
D. #ifelse
ANSWER: D
Explanation: #ifelse is not a preprocessor directive. #error, #pragma, #if are preprocessor
directives. There is a preprocessor directive, #elif, which performs the function of else-if.

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 35


What will be output of the following C code?
#include <stdio.h>
#define x 5+10
int main()
{
int result = 10;
result = result * x;
printf("%d", result);
return 0;
}
A. 150
B. 60
C. Compile time error
D. None of the mentioned
ANSWER: B

36 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following C Code?
#include <stdio.h>
#define FALSE -1
#define TRUE 1
#define NULL 0
int main()
{
if (NULL)
printf("NULL");
else if (FALSE)
printf("TRUE");
else
printf("FALSE");
return 0;
}
A. NULL
B. TRUE
C. FALSE
D. None of the mentioned
ANSWER: B

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 37


What is the output of this C code?
#include <stdio.h>
#define rec(m, n) " m ## n "
int main()
{
printf("%s", rec(k, l));
return 0;
}
A. k 1
B. k1
C. Compile time error
D. m ## n
ANSWER: D

38 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What is the output of the following C code?
#include <stdio.h>
#define rec(x, y) #x #y
int main()
{
printf("%s", rec(k, 1));
return 0;
}
A. k1
B. k 1
C. xy
D. Compile time error
ANSWER: A

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 39


What is the output of the following C code?
#include <stdio.h>
#define rec(x, y) x / y + x
int main()
{
int i = -6, j = 3;
printf("%d", rec(i + j, 3));
return 0;
}
A. Divided by zero exception
B. Compile time error
C. -8
D. -4
ANSWER: C

40 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


Assume sizeof an integer and a pointer is 4 bytes. Output of the following program?

#include <stdio.h>
#define M 6
#define N 4
int main()
{
int (*p)[M][N];
printf("%d", sizeof(*p));
return 0;
}

A. 96
B. 24
C. 6
D. 4
ANSWER: A

Output is 6*4*sizeof(int) which is “96” for compilers with integer size as 4 bytes. When a
pointer is de-referenced using *, it yields type of the object being pointed. In the present case,
it is an array of array of integers. So, it prints R*C*sizeof(int).

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 41


What is the output of the following program?

#include <stdio.h>
#define a 5
int main()
{
int a = 2, b = 3;
a = a + b;
b = a - b;
a = a - b;
printf("%d, %d", a, b);
return 0;
}

A. 2, 3
B. 3, 2
C. 5, 2
D. Compilation error

ANSWER: D

#define is a pre-processor and a is replaced by 5 before compiling. Thus ’a’ cannot be declared
as a variable. Thus, the compiler will return compilation error.

42 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What is the error in the following program?

#include<stdio.h>
#define SI(p, n, r) float si; si=p*n*r/100;
main() {
float p = 2500, r = 3.5, a;
int n = 3;
a = SI(p, n, r);
SI(1500, 2, 2.5);
printf("%f", a);
}

A. Variable 'a' should be replaced by 'si'


B. Nothing will print
C. Error in declaration of si
D. No error

ANSWER: C

The macro #define SI(p, n, r) float si; si=p*n*r/100; contains the error. To remove this error,
we have to modify this macro to #define SI(p,n,r) p*n*r/100

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 43


What will be the output?

#include<stdio.h>
#define X 3
int main()
{
#if !X
printf("hello");
#else
printf("world");
#endif
return 0;
}

A. hello
B. world
C. compiler error
D. run time error

ANSWER: B

44 [Link] | AP (SG) | CSE | Rajalakshmi Engineering College


What will be output of the following C code?

#include <stdio.h>
#define z 5;
int main()
{
z = z + 5;
printf("%d", z);
return 0;
}

A. 5
B. 10
C. 0
D. Compile time error

ANSWER: D

[Link] | AP (SG) | CSE | Rajalakshmi Engineering College 45

You might also like