Sunday, 15 June 2014

#13. Concepts in C -- only through Examples & Explanations

This is a continuation of  post #11. 

Before seeing the answer try to predict it & then see the explanation.

All the Best!!

26) main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}

27) main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2)
goto here;
i++;
}

}
fun()
{
here:
printf("PP");
}

28) main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}

29) void main()
{
int i=5;
printf("%d",i++ + ++i);
}

30) void main()
{
int i=5;
printf("%d",i+++++i);
}

===================================================
26.
Answer:
H
Explanation:
* is a dereference operator & is a reference operator. They can be applied
any number of times provided it is meaningful. Here p points to the first
character in the string "Hello". *p dereferences it and so its value is H. Again
& references it to an address and * dereferences it to the value H.

27.
Answer:
Compiler error: Undefined label 'here' in function main
Explanation:
Labels have functions scope, in other words the scope of the labels is limited
to functions. The label 'here' is available in function fun() Hence it is not
visible in function main.

28.
Answer:
Compiler error: Lvalue required in function main
Explanation:
Array names are pointer constants. So it cannot be modified.

29.
Answer:
Output Cannot be predicted exactly.
Explanation:
Side effects are involved in the evaluation of i

30.
Answer:
Compiler Error
Explanation:
The expression i+++++i is parsed as i ++ ++ + i which is an illegal
combination of operators.

To be continued...
========================================================

Thanks for coming till this point!!
VSG
SQL DBA


No comments:

Post a Comment