See C

Well having attended a few C debugging contests, I found C can get very interesting even at the very basic levels. Here is a few questions that are worth understanding if you got any interest in C :)

*)First is the simple printf statement. Consider
int i=5;
printf("%d,%d", i, ++i);

If you guessed output as 5,6 - sorry.

Thing is printf first evaluates all the expression from RIGHT TO LEFT before printing the actual values. So actual answer would 6,6.

*)Now the comma operator. They always asked questions involving this. I didn't even know what it meant till I saw this.

*)The playing with increment/decrement operator. Consider
i=5;
j=i+++++i;

If you split the second line as j= (i++) + (++i) and calculated the appropriate value, then congrats, you made the same mistake as me :)
Seems it actually gets divided into (i++) ++ (+i) so it becomes an invalid expression.

*)Problem is to print "Hello World" without a single semi colon in the program :D

Now I thought of macros,recursion,function calls blah blah blah but as always the answer was one hell of a simple solution.
Just put the printf statement in the if condition or for/while condition thing and job done :)

*)The mother of all C questions :D
int a,i=5;
int *b=&i; //Creating pointer to i
a = i/*b;
What is value of a?
If the answer 1 popped up in your mind (after all we divided a value by same value), ha ha ha! Compile,run and see :D

No comments: