Tuesday 23 April 2013

#define max 5;


What will be output if you will compile and execute the following c code?

#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}


(A) 5

(B) 6

(C) 7

(D) 0

(E) Compiler error


________________________________________
Explanation:
#define is token pasting preprocessor. If you will see intermediate file: test.i
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5++;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:
It is clear macro constant max has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.

int vs static int


What will be output if you will compile and execute the following c code?

void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}


(A) Equal

(B) Greater than

(C) Less than

(D) Compiler error

(E) None of above


________________________________________
Explanation:
static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable.
In this example i is run time variable while x is load time variable.

What is error in following declaration?


What is error in following declaration?

struct outer{
int a;
struct inner{
char c;
};
};


(A) Nesting of structure is not allowed in c.

(B)
It is necessary to initialize the member variable.

(C) Inner structure must have name.

(D) Outer structure must have name.

(E) There is not any error.


________________________________________
Explanation:
It is necessary to assign name of inner structure at the time of declaration other wise we cannot access the member of inner structure. So correct declaration is:
struct outer{
int a;
struct inner{
char c;
}name;
};

char c='\08';


What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
char c='\08';
printf("%d",c);
}



(A) 8

(B) ’8’

(C) 9

(D) null

(E) Compiler error


________________________________________
Explanation:
In c any character is starting with character ‘\’ represents octal number in character. As we know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7. So 8 is not an octal digit. Hence ‘\08’ is invalid octal character constant.

sizeof()


What will be output if you will compile and execute the following c code?

void main(){
printf("%d",sizeof(5.2));
}


(A) 2

(B) 4

(C) 8

(D) 10

(E) Compiler error


________________________________________
Explanation:
Default type of floating point constant is double. So 5.2 is double constant and its size is 8 byte.

for loop example with semicolon.



What will be output if you will compile and execute the following c code?

void main(){
  int x;
  for(x=1;x<=5;x++);
    printf("%d",x);
}


(A) 4

(B) 5

(C) 6

(D) Compiler error

(E) None of above


________________________________________
Explanation: 6
Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false.