Monday, 29 February 2016

Tricky C Programs

Some tricky C Programs

Predict the output of the following code and the reason.
1. 
int main()
{

printf("%d",printf("computer"));

return (0);

}

Ans: computer8
Reason: inner printf() is called first and it returns int which is the number of character it prints. So returns 8. Lastly, the outer printf() gets executed.

2.
int main()
{
  int i;

  for(i=0;i<5;i++)
    {
      int i = 10;
 
        printf("%d",i);
    }
  return (0);
}

Ans: 1010101010
Reason: i is newly initialized inside the for loop body which has no impact on the outer one. As a result the loop does'nt terminates and iterates 5 times.

No comments:

Post a Comment