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.

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

Sunday 14 April 2013

Pointer Variables Program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1

(B) 5

(C) -5

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: 1

Turbo C ++4.5: 1

Linux GCC: 1

Visual C++: 2



Difference of two same type of pointer is always one.

Register Variable Program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
//if a=25

(A) 25

(B) Address

(C) 0

(D) Compilation error

(E) None of above


________________________________________
Explanation:
Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error



Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

Void Pointer Program



#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10

(B) Address

(C) 2

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: 10

Turbo C ++4.5: 10

Linux GCC: 10

Visual C++: 10



Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

Register variable program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}
(A) 25

(B) 4

(C) Address

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error



Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

Saturday 13 April 2013

#define



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

Operator's tricky program 12


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

Explanation
Output: 
Turbo C++ 3.0: 21

Turbo C ++4.5: 21

Linux GCC: 22

Visual C++: 24


Explanation:
Rule : ++ (in ++i) is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole equation up to break point then start assigning the value of variable in the equation. There are many break point operators in. For example:

(1) Declaration statement.
(2) && or operator.
(3) Comma (,) operator etc.

In the following expression:
int a=++i + ++i + ++i;

Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on.
So, a = 6 + 7 + 8;

Operator's tricky program 11


What will be output of the following program?

#include<stdio.h>
int main(){
    float a;
    (int)a= 45;
    printf("%d,a);
    return 0;
}

Explanation
Output:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error

Explanation:
After performing any operation on operand it always return some constant value.

(int) i.e. type casting operator is not exception for this. (int) a will return one constant value and we cannot assign any constant value to another constant value in c.

(int)a = 45; is equivalent to
3456 = 45 ( Here 3456 in any garbage value of int(a)).

Operator's tricky program 10


What will be output of the following program?

#include<stdio.h>
int main(){
    int a;
    a=sizeof(!5.6);
    printf("%d",a);
    return 0;
}

Explanation
Output:
Turbo C++ 3.0: 2
Turbo C ++4.5: 2
Linux GCC: 4
Visual C++: 4

Explanation:
! is negation operator it return either integer 0 or 1.
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
Since 0 is integer number and size of integer data type is two byte.

Operator's tricky program 9


What will be output of the following program?

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

Explanation
Output:
Turbo C++ 3.0: 5 20 100

Turbo C ++4.5: 5 20 100

Linux GCC: Garbage values

Visual C++: 5 100 20

By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order of declaration. So output will be 5 20 100.

Friday 12 April 2013

Operator's tricky Program 8


What will be output of the following program?

#include<stdio.h>
int main(){
    printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
    return 0;
}

Explanation
Output: 
Turbo C++ 3.0: 8 4 10

Turbo C ++4.5: 8 4 10

Linux GCC: 8 4 12

Visual C++: 8 4 8

Explanation:
3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.

Operator's tricky Program 7


What will be output of the following program?

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

Explanation
Output: 
Turbo C++ 3.0: 131

Turbo C ++4.5: 131

Linux GCC: 131

Visual C++: 131


Explanation:
015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13
0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113
So, a = 13 + 113 + 5 = 131

Operator's tricky Program 6


What will be output of the following program?

#include<stdio.h>
int main(){
    int a=0,b=10;
    if(a=0){
         printf("true");
    }
    else{
         printf("false");
    }
    return 0;
}

Explanation
Output: 
Turbo C++ 3.0: false

Turbo C ++4.5: false

Linux GCC: false

Visual C++: false


Explanation:
As we know = is assignment operator not relation operator. So, a = 0 means zero will assigned to variable a. In c zero represent false and any non-zero number represents true.
So, if(0) means condition is always false hence else part will execute.