#include <stdio.h>
int main()
{
        int i=43;
        printf("%d\n",printf("%d",printf("%d",i)));
        return 0;
}

Dani AI

Generated

Short answer: yes — the program is valid C and, with typical library behavior, prints 4321 followed by a newline. The nested calls are evaluated so the innermost printf runs first, its return value becomes the next call's argument, and so on; each printf returns an int (the count of characters printed, or a negative value on error).

Step-by-step: the inner call prints the decimal digits of i (for 43 that is two characters) and returns 2; the middle call prints 2 and returns 1; the outer call prints 1 and a newline. This evaluation order follows the language rules that function-call arguments must be evaluated before the call is made — nested calls force the inner evaluations first (evaluation order reference). The standard library printf return-value behavior is documented in implementation manuals (for example, the POSIX/Linux man page) and is the basis for the numeric values printed by the middle and outer calls (printf return description).

Clarifications tied to earlier replies: is right the program is valid, but the idea that missing arguments are magically filled from “dummy memory” is misleading — if a format expects an argument that wasn’t passed, the behavior is undefined (it might appear to work on some platforms or crash on others). and are correct that printf returns an int (not void). As noted, modern compilers will often warn about mismatches — compile with -Wall -Wformat (or equivalent) to catch format/argument problems.

Practical caution: this pattern is valid here because types and counts match. Avoid relying on nested side effects or unspecified ordering in more complex expressions; prefer clearer code when maintainability matters.

Recommended Answers

All 6 Replies

Does it compile and run?
Read the documentation of printf, and you will understand if it is a valid program or not.

Yes it is valid

because printf return number of written characters. but if printf did not return a value, this program would be valid.

this case is alittle hard to explain. %d in formatting string will look for suitable parameter, but if can not find it will put a dummy data availbale in memory so this program will work or it is valid.

but if printf did not return a value, this program would be valid.

If printf didn't return a value, it would return void. There aren't any values of void type, so something like this shouldn't ever compile. That means it's not valid.

#include <stdio.h>

void dummy( void ) { }

int main( void ) {
  printf( "%d\n", dummy() );

  return 0;
}

%d in formatting string will look for suitable parameter, but if can not find it will put a dummy data availbale in memory so this program will work or it is valid.

I thought that if there aren't enough arguments or if the arguments are a type that don't match the formatting string, the behavior of printf is undefined. I think that's how it works, but I'm not a guru. Can you show me where the standard document supports your explanation?

If printf didn't return a value, it would return void. There aren't any values of void type, so something like this shouldn't ever compile. That means it's not valid.

printf() always returns an int. Not a void type. If an error happens it will return a negative integer for EOF.

printf() always returns an int. Not a void type.

Yeah, I know. You should read my post again, and the post above it, more slowly this time. ;)

Yes, if you don't give printf() the correct arguments then its behavior is undefined. But most likely it will assum the stack contains the correct arguments and use whatever is there. That's why a lot of people get core dumps or access violations then they have "%s" in the format string but fail to pass a valid pointer to a character array.

As I mentioned in another thread, some compilers will validate the parameters to printf() and issue warnings is they are not correct. I wish all compilers did that :)

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.