Friday, December 4, 2009

Questions about printf

1. Look at the code below (funny question)

void main() { 

   if(X) 

  { 

     printf("Hello"); 

  } 

  else 

  { 

     printf(" World"); 

  } 

} 
 What should X be replaced with inorder to get the output as "Hello World"?
And here comes the answer....
#include  

int main(){ 

    if(!printf("Hello")) 

    { 

        printf("Hello"); 

    } 

    else 

    { 

        printf(" World"); 

    } 

} 

2.
#include
int main()
{
int i=448;
printf("%d\n",printf("%d",printf("%d",i))); return 0;
}

Output: 44831

3.

main()
{
int i=0;
for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:
1
Explanation:
before entering into the for loop the checking condition is "evaluated".
Here it evaluates to 0 (false) and comes out of the loop, and i is
incremented (note the semicolon after the for loop).

0 comments:

Post a Comment