c coding questions and answer

Best questions of preprocessor with answer

(1)
#include<stdio.h>
#define division 10\3
int main(){
    printf("%d",division); 
    return 0;   
}

What will be output if you compile and execute the above code?
(a)3
(b)4
(c)103
(d)Compiler error

(2)
#include<stdio.h>
void display();
void calculate();
#pragma startup calculate
#pragma  exit display
int main(){
    printf("\ncquestionbank");
    return 0; 
}
void calculate(){
    printf("\njava-questionsbank");
}
void display(){
    printf("\njavadoubt");   
}

What will be output if you compile and execute the above code?
(a)cquetionbank
   Java-questionsbank
   javadoubt
(b)java-questionban
   Cquetionbank
   javadoubt
(c) cquetionbank
(d)Compiler error

(3)
#include"stdio.h"
#define sizeof(int) 4
int main(){ 
     int *p=NULL;
     printf("%d" ,p+1);
     return 0; 
}

What will be output if you compile and execute the above code?
(a)2
(b)4
(c)Garbage value
(d)Null pointer error

(4)
#include<stdio.h>
#define int double
#define const struct
#define char sizeof
const student{
    int s;
};
int main(){
    const student s;
    printf("%d" ,char(s));  
    return 0; 
}

What will be output if you compile and execute the above code?
(a)1
(b)8
(c)Run time error
(d)Compiler error

(5)
#include<stdio.h>
#define cube(x) (x*x*x)
int main(){
    int x=2,y,z;
    y=cube(++x);
    z=++y+386/cube(++x);
    printf("%d %d %d",++x,y,z);
    return 0; 
}

What will be output if you compile and execute the above code?
(a)9 126 126
(b)4 27  126
(c)4 27  92
(d)Compiler error

(6)
#include<stdio.h>
#define find(a,b) a##b
int main(){
    int a=10;
    int b=20;
    int ab=15;
    printf("%d",find(a,b)-ab);
    return 0; 
}

What will be output if you compile and execute the above code?
(a)0
(b)5
(c)1005
(d)Compiler error

(7)
#include<stdio.h>
#define max 0\5
int main(){
    #if max
         printf("cquestionbnk");
    #elif max+1
         printf("java-questionsbank");
    #else
         printf("blogspot.com");
    #endif
    return 0; 
}

What will be output if you compile and execute the above code?
(a)cquestionbank
(b)java-questionsbank
(c)blogspot.com
(d)Compiler error

(8)
#include<stdio.h>
#define option1 a++;\
printf("%d",a);
#define option2 printf("%d",a);
int main(){
    int a=10;
    if(a++)
           {option1}
    else
         option2  
    return 0; 
}

What will be output if you compile and execute the above code?
(a)10
(b)11
(c)12
(d)Compiler error

Answer of preprocessor questions:
1. (c)
2. (b)
3. (a)
4. (b)
5. (a)
6. (a)
7. (a)
8. (c)

13 comments:

mayank said...

first question is throwing a error i think 10\3 should be replaced by 10/3

Priyanka kumari said...

Hi Manyank,

You may right. As you know c is compiler dependent language. First question will work good in turbo c 3.0 but it will throw an error in turbo c 4.5 compiler.
Hints: \ is preprocessor new line character. so,
#define division 10\3
is equivalent to
#define division 103

Unknown said...

pls explain the prog no 3 thoroughly...here i could not understand the pointer use and preprocessor......

Unknown said...

pls explain prog5 how ++x,y,z o/p is 9,126,126 respectively

manish said...

There is no use of preprocessor at all it is just printing address at which p is pointing.

Anonymous said...

it is very useful keep up the good work....

Anonymous said...

Pls provide the explanation for the solution,
difficult to understand.

karunyadhandapani said...

Please anyone tell about the use of '\' operator in #define

dinhpq said...

Thanks.

pratyush said...

Q.5 Solution:
x=2;
y=cube(++x);
--> y=(5x5x5)=125 {now x=5;}

z=++y+386/cube(++x);
--> z=126+386/(8x8x8) {now x=8;} {now y=126;}
--> z=126+0=126;

printf("%d %d %d",++x,y,z);
Ans: 9,126,126

//-----------
Note this case:

x=2;
y=cube(x++); // y=(2x2x2) {now x=5;}
printf("%d %d",x,y);

Ans:5 8

Anonymous said...

@pratyush
x=2;
y=cube(++x);//at ds step it must be x=3,then why it is 5
--> y=(5x5x5)=125 {now x=5;}

Anonymous said...

Plzz also provide the explaination of the answers ....sometimes it's difficult to understand the answer....plzzz

Unknown said...

@5
you have to replace each X by ++X
so cube(++x) (++x*++x*++x) and that means x=5 at first place then 8