[Solved] Output of Nested printf and scanf in C [closed]


I think what you are missing is, your one line with nested function calls is basically same as this code:

int scanf_result = scanf("%d%d,",&i,&a);
int printf_result = printf("PRINT %d\t", scanf_result));
printf("%d", printf_result);

scanf call should return 2 if you entered valid input, but can also return -1 for actual error, or 0 or 1 if it failed to scan 2 integers. This is then printed by first printf as you’d expect, and it should return 8 (or 9 if scanf returned -1), so that 7 probably means difference between code you actually executed, and code you pasted… Then 2nd printf prints that number on the same line (no newline printed anywhere), giving you the final output.

There is no ambiguity, compiler can’t do things in different order here. When you pass return value of a function as argument to another function, the first function has to be called first, to get the return value. So any “side-effects” such as reading from standard input or printing to standard output also happen in that order.

The way you can get ambiquity is to use assignment operators or ++ or -- operators multiple times on same variables inside same argument list. Then it is undefined when variable values actually change, and therefore which values get passed, when you use that same variable many times inside same argument list.

0

solved Output of Nested printf and scanf in C [closed]