Thursday 18 April 2013

printf("%d",printf("%s",str));


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

void main(){
   char *str="Hello world";
   printf("%d",printf("%s",str));
}


(A) 11Hello world

(B) 10Hello world

(C) Hello world10

(D) Hello world11

(E) Compiler error


________________________________________
Explanation:
Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf(“Hello world”) will return 11.

printf("%d %d %d",a,a++,++a);


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

void main(){
  int a=10;
  printf("%d %d %d",a,a++,++a);
}


(A) 12 11 11

(B) 12 10 10

(C) 11 11 12

(D) 10 10 12

(E) Compiler error


________________________________________
Explanation:
In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction.

So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12.

Misplaced break statement error


What will be output if you will compile and execute the following c code?
void main(){
 int a=2;
 if(a==2){
   a=~a+2<<1;
   printf("%d",a);
 }
 else{
  break;
 }
}


(A)
It will print nothing.

(B)
-3

(C)
-2

(D)
1

(E)
Compiler error



Explanation:
Keyword break is not part of if-else statement. Hence it will show compiler error: Misplaced break

char data type cycle


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

void main()
{
char c=125;
    c=c+10;
    printf("%d",c);
}


(A) 135

(B) +INF

(C) -121

(D) -8

(E) Compiler error

________________________________________
Explanation:
As we know char data type shows cyclic properties i.e. if you will increase or decrease the char variables beyond its maximum or minimum value respectively it will repeat same value according to following cyclic order:

So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121

#define Program


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

#define x 5+2
void main(){
    int i;
    i=x*x*x;
    printf("%d",i);
}


(A) 343

(B) 27

(C) 133

(D) Compiler error

(E) None of above


________________________________________
Explanation:
As we know #define is token pasting preprocessor it only paste the value of micro constant in the program before the actual compilation start. If you will see intermediate file you will find:
test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the 5+2 in place of x in program. So,
i=5+2*5+2*5+2
=5+10+10+2
=27

Address of Pointers.


What will be output of following program?

#include<stdio.h>  
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
}
(A) 3 Address 3

(B) 3 Address Address

(C) 3 3 3

(D) Compilation error

(E) None of above



Explanation:
Turbo C++ 3.0: 3 Address Address

Turbo C ++4.5: 3 Address Address

Linux GCC: 3 Address Address

Visual C++: 3 Address Address

Here 6024, 8085, 9091 is any arbitrary address, it may be different.

& operator program


What will be output of following program?

#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}
(A)
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address

(B)
value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address

(C)
value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0:

value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address  

Turbo C ++4.5:

value of i : 100 addresss of i : Address

value of i : 101 addresss of i : Address  

Linux GCC:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address  
Visual C++:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address  


Within the scope of any variable, value of variable may change but its address will never change in any modification of variable.

& and * program


What will be output of following program?
#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address

(B) Address Address

(C) Address 5

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: Address Address

Turbo C ++4.5: Address Address

Linux GCC: Address Address

Visual C++: Address Address



Since * and & always cancel to each other.

i.e. *&a = a

so *&p = p which store address of integer i

&*p = &*(&i) //since p = &i

= &(*&i)

= &i

So second output is also address of i