Thursday, December 25, 2008

Operator Question Answer with Detail Explanation in C program

Q1.what will output :-
void main()
{
float a=0.7;
if(a<0.7)
printf("C");
}
else
{
printf("C++");
}
}

output: c
explanation :
0.7f is float constant. Its binary value is written in 32 bit.
0.7 is double constant(default). Its binary value is written in 64 bit.
0.7L is long double constant. Its binary value is written in 80 bit.
binary value of 0.7=(0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011)
now here a is a float variable while 0.7 is double constant .so a contain only 32 bit value
i.e
a=0.1011 0011 0011 0011 0011 0011 0011 0011
while
0.7=0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a<0.7>

Q2.what will output :-
void main()
{
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
}

output: 8 24
explanation :
rule :- ++ is preincrement operator so in any arithmetic operation it first increment the value of variable by one in
whole equation then start assingning the end value of variable in the equation . j=++i + ++i + ++i;
initial value of i=5 after three times increment i=8 now final value of i i.e. 8 will assigne to each . so j=8+8+8; j=24; i=8;

Q3.what will output :-
void main()
{
int i=1;
i=2+2*i++;
printf("%d",i);
}
output: 5
explanation :
i++ i.e when postfix is used with variable in expression then expression is evaluated first with original value then
variable is incremented. so i=2+2*1 i=4 now i will be incremented by one so i=4+1=5

Q4.
what will output :-
void main()
{
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
}
output: 0
explanation :
== is relation operator which will give only two value they are 0 : if a==b is false 1 :
if a==b is true now a=2 b=7 so a==b is false hence b=0

Q5.
what will output :-
void main()
{
int x;
x=10,20,30;
printf("%d",x);
}
output: 10
explanation :
comma operator (,) has least precedence and its associativity is from left to right so assignment operator (=) has
more precedence than comma operator .So = operator will be evaluated first than comma operator x = 10 , 20 , 30
precedence 1 2 3 first x =10 will be evaluated then control will transfer to 20 then 30.

Q6.
what will output :-
void main()
{
int a=0,b=10;
if(a=0)
{
printf("true");
}
else
{
printf("false");
}
}
output: false
explanation :
if(a=0) then a=0 so if(0) means false if(1) means true note : if(n) means true, where n is any number either positive or
negative except zero

Q7.
what will output :-
void main()
{
int a;
a=015 + 0x71 +5;
printf("%d",a);
}
output: 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

Q8.
what will output :-
void main()
{
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
}
Output: 8 4 10
explanation :
3.14f is float 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.

Q9.
what will output :-
void main()
{
int x=100,y=20,z=5;
printf("%d %d %d");
}
Output: 5 20 100
By default x,y,z are auto type data which stores in stack in memory.
Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter (2 byte)
points top element i.e 5 .Default value of %d in printf is data which is persent in stack.
So output is revere order of dclaration. So output will be 5 20 100.
note. Initialize variables are more near then uninialize variable.

Q10.
what will output :-
void main()
{
int a=2;
a=a++ + ~++a;
printf("%d",a);
}
output: -1
Explanation: Same theory as question (2) and (13).

Q11.
What will be output :
void main()
{
int a;
a=sizeof(!5.6);
printf(“%d”,a);
}
Output : 2
Explanation: ! is negation operator it return either integer 0 or 1.
!operand=0 if operand is non zero.
!operand=1 if operand is zero.
So !5.6=0 ,since 0 is integer number and size of integer data type is two byte.

Q12.What will be output :
void main()
{
float a;
(int)a= 45;
printf(“%d”,a);
}
Output : error
Explanation: After using any operator on operand it always return some integer value. (int) i.e
type casting operator is not exception for this. (int) a is converted in any integer value and we cannot assign any
constant value to another constant value in c .So
(int)a =45; is equivalent to
3456=45 .( here 3456 in any garbage value of int(a)).
Q13.What will be output of
void main()
{
int i=5;
int a=++i + ++i + ++i;
printf(“%d”,a);
}

output : 21
explanation : rule :- ++ is preincrement operator so in any arithmetic operation it first increment the value of
variable by one in whole equation upto break point then start assingning the value of variable in the equation .
There are three break point.
(1) declaration or intialization statement.
(2) && or operator.
(3) comma (,) operator.
int a=++i + ++i + ++i;
here break point is due to declaration and inialization operator .
It break after each increment i.e ( initial value of i=5)
after first increment value 6 assign to i then next incrementation occur and so on . so a=6+7+8;



No comments:

Blog List