C Puzzles

int main()
{
int i=567;
printf(“%d\n”,printf(“%d”,printf(“%d”,i)));
return 0;
}
Output: 56731
Reason:( ) have higher precedence, first the innermost printf statement prints the value assigned to i, i.e. 567. The second printf then counts the number of characters previous printf statement; value of i[i.e.3] and the outermost printf statement counts no. of characters of previous printf statement i.e.1


int main()
{
int *i=50;
printf(“%d\n”,i);
return 0;
}
Output: 50
Reason: prints value assigned to i, which is equal to 50.


int main()
{
int *i=50;
i++;
printf(“%d”,i);
getch();
return 0;
}
Output: 54 or 52 depending on size of int used by respective compiler is 4 or 2.
Reason: increment a pointer whose value is 50. It goes to the next address location, which is 50 + sizeof(data pointed) = 50 + sizeof(int).


int main()
{
char i=’ABCZa’;
printf(“%d”,i);
return 0;
}
Output: 97
Reason: prints the ASCII value of ‘a’.


int main()
{
int p;
int *a=&p;
scanf(“%d”,a);
printf(“%d”,p+5);
return 0;
}
Output: prints value of a+5
Reason: value at a has address of p assigned to it. The program first scans value of a and prints value of a(though, it is p+5; value of a holds address of p) by adding constant 5 to it.


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s