0

Hi i hav executed the following c code..the inner printfs executes first and prints some garbage value...why dont the outer printf prints anything...
i got only one garbage value y dont two garbage values....wt s the order of precedence in printf function and in nested printfs....

the c code is

void main()
{
 while(1)
 {
  if(printf("%d",printf("%d")))
  break;
    else
    continue;
 }
}

Thanks & Regards.

Edited by Nick Evan: n/a

5
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by ~s.o.s~
0
if(printf("%d",printf("%d")))

The second printf doesn't get a variable to print, the second paramter is missing

Edited by Nick Evan: n/a

0

I get two values not one.
BTW printf returns the number of characters it has printed and the inner statement is always executed first though it is not wise to assume the order of execution.

Try this program:

int main()
{
    while(1)
    {
        if(printf("%d",printf("    %d"))) // the inner printf prints 5 chars and hence returns 5
            break;
        else
            continue;
    }
}

//My output:
//    05
0

I bet it is probably a puzzle given by the professor to confuse the students in which case it is not necessary for the inner printf to print something meaningful.
I think he wanted to stress the point that printf returns a value and that is equal to the number of characters printed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.