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.

Recommended Answers

All 5 Replies

if(printf("%d",printf("%d")))

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

use int main instead of void main.

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

The inner printf is missing an int parameter to make the %d do something meaningful!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.