Wednesday, February 22, 2012

C question set 6


1=

main()
{
int i = 3;
for (;i++=0;) printf("%d",i);
}
Answer:

Compiler Error: Lvalue required.
Explanation:

As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an
assignment operation.

2=

main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Answer:

10 10
Explanation:

The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
      {
if(i,j)
    j = i;
else
   j = j;
}
  else
j = j;

3=

const char *a;
char* const a;
char const *a;

-Differentiate the above declarations.
Answer:

'const' applies to char * rather than 'a' ( pointer to a constant char ) 
*a='F' : illegal 
a="Hi" : legal
'const' applies to 'a' rather than to the value of a (constant pointer to char ) 
*a='F' : legal 
a="Hi" : illegal
Same as 1.

4=

main()
{
int i=4,j=7;
j = j || i++ && printf("YOU CAN");
printf("%d %d", i, j);
}
Answer:  4 1


Explanation:

The boolean expression needs to be evaluated only till the truth value of the expression is not known.
 j is not equal to zero itself means that the expression's truth value is 1. Because it is followed by || and
true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not
evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression,
 when any of the operands become false, the whole expression's truth value becomes false and hence the
 remaining expression will not be evaluated.
false && (anything) => false where (anything) will not be evaluated.

5=

main()
{
register int a=2;
printf("Address of a = %d",&a);
printf("Value of a   = %d",a);
}
Answer:

Compier Error: '&' on register variable 
Rule to Remember:
& (address of ) operator cannot be applied on register variables.

1 comment: