Search Placementyogi.com
 

C Preprocessor Objective Questions and Answers

  :: Topics ::

1

Identify macro definition in below c program.

#define MAX 2                 //LINE 1
#define SQUARE(x) (x*x)       //LINE 2
void main()
{
  int y = MAX * SQUARE(4);    //Line 5
}
A.

Line 1 is macro definition

B.

Line 2 is macro definition

C.

Both Line 1 & Line 2 are macro definitions

D.

None is macro definition

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

Both Line 1 and Line 2 are macro definitions. They comprise of 'macro template' MAX and SQUARE(x) and their corresponding 'macro expansion', 2 and (x*x).

During preprocessing before compilation , the macro templates are replaced by their corresponding macro expansions.

 

So in above program  line 5 before compilation is =>  int y = 2 * (4*4);

 
2

What is the output of program?

#include<stdio.h>
#define MAX            //LINE 2
int main()
{

int x =4 * MAX;       //LINE 5

printf("x=%d",x);

}
A.

0

B.

4

C.

error at LINE 2 : macro definition expected

D.

error at LINE 5 : expected expression before ; token

See Answer & Explanation Lets Discuss
Correct answer is : D
Explanation

 

We can define MAX at line 2 without definition so no error at Line 2. 

There is no definition for MAX so in program during preprocessing at Line 5 MAX will be replaced by space.

 

So the Line 5 sent to compiler is :  int x = 4 * ;

 

and compiler generates error : 'expected expression before ; token'

 

 
3

What is the output of below program?

void func()
{
 #define SQUARE(x) (x)*(x)  
}

int main()
{
 int x = SQUARE(2);
 printf("x=%d",x);

 return 0;
}
A.

x=4

B.

error : definition of macro is not allowed inside function

C.

error : Scope of SQUARE is limited to func() so undefined SQUARE macro in main()

D.

None

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

 

All the macros are processed by preprocessor before compilation. Preprocessor starts processing code from top to bottom, so macro definition should be placed before it's use any where in code file.

 

Suppose above code is in file example.c

 

So after preprocessing it looks like below code( cmd used=> gcc -E example.c -o example.i)

void func()
{
                 // macro definition is deleted after preprocessing
}

int main()
{ 
 int x = 2*2;   // macro template is replaced is its definition
 printf("x=%d",x);
 return 0;
}

So compiling above code gives no error and prints 4.

 
4

What is the output of below program?

#include<stdio.h>
#define int char      // Line 2
int main()
{
 int* str="Hello";    // Line 5
 printf("%s",str);

 return 0;
}
A.

error : use of keyword in macro definition is not allowed at line 2

B.

Warning : initialization from incompatible pointer type at line 5

C.

Prints Hello without any warning and error

D.

Run time error

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

We can use keywords in macro definition because keywords does not have any meaning in preprocessing phase.So no error at Line 2. 

 

At Line 5 int will be replaced by char so no warning msg for initialization from incompatible pointer type

 
5

What is the output of below program?

#include<stdio.h>

int main()
{
 int x = SQUARE(2);
 printf("x=%d",x);

 return 0;
}
void func()
{
 #define SQUARE(x) x*x  
}
A.

4

B.

error : definition of macro is not allowed inside function

C.

error : undefined SQUARE macro in main()

D.

None

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

All the macros are processed by preprocessor before compilation. Preprocessor starts processing code from top to bottom, so macro definition 

should be placed before it's use any where in code file.

 

Suppose above code is in file example.c

 

So after preprocessing it will look like below code( cmd used=> gcc -E example.c -o example.i)

int main()
{

 int x = SQUARE(2);  //Line 3, macro template is not replaced 

 printf("x=%d",x);
 return 0;
}


void func()
{
                    //macro definition is deleted after preprosessing
}

In main macro is not replaced with its definition because when preprocessor was processing line 3 it was not aware of macro SQUARE definition which id defined in func() below the main().

So during compilation it generated undefined SQURAE at line 3 

 
 

Banner to LogicGuns.com

Hide Page Information

C Preprocessor Objective Questions and Answers

C Preprocessor Objective Questions and Answers

Here you can find Preprocessor in C MCQ questions with answers and explanation.

Why Preprocessor in C?

In this section you can learn and practice Preprocessor in C (Questions with Answers) to improve your skills in order to face the interview, competitive examination and various entrance test of TCS, Infosys, Wipro, Coznizant interview with full confidence.

Where can I get Preprocessor in C Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Preprocessor in C questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Preprocessor in C quiz questions with answers as PDF files and eBooks.

Where can I get Preprocessor in C Multiple Choice Questions and Answers (objective type, multiple choice)?

Here you can find objective type Preprocessor in C questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve Preprocessor in C problems?

You can easily solve all kind of questions based on Preprocessor in C by practicing the exercises (including shortcut methods to solve problems) given below.