int a = 5;
#include <stdio.h>
int main() {
printf("%d", printf("hi!") * printf("bye"));
return 0;
}
Output:
hi!bye9
I would like to know how the order in which the output has occurred. Does this mean printf
function returns a value?
What is the reason behind inner printf
statements being executed first?
Input/Output The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.
This statement is not correct but, you can use printf within printf. Upon successful return, printf functions return the number of characters printed (not including the trailing '\0' used to end output to strings). Save this answer.
The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio. h (header file).
One, the printf (short for "print formatted") function, writes output to the computer monitor. The other, fprintf, writes output to a computer file. They work in almost exactly the same way, so learning how printf works will give you (almost) all the information you need to use fprintf.
This program exhibits unspecified behavior since the order of evaluation of sub expressions is unspecified except where it is specifically defined:
printf("%d",printf("hi!")*printf("bye"));
^ ^
1 2
So either 1 or 2 could be evaluated first and you can not determine which. We can see this from the C99 draft standard section 6.5
Expressions paragraph 3 which says (emphasis mine going forward):
The grouping of operators and operands is indicated by the syntax.74) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
Yes, printf does have a return value, which is the number of character printed or -1
if there is an error, in this case assuming no error the return value will be 3
for both the inner printf
s
The arguments to a function are evaluated before the function is called which is why the inner printf
s are executed first, this is covered in section 6.5.2.2
Function calls paragraph 4:
An argument may be an expression of any object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.81)
The order of the internal printf
s is unspecified. Another implementation, the same implementation with different compiler settings, or the same implementation with the same code in a different place might produce byehi!9
.
The 9 comes because printf
returns the number of characters printed, so the two internal printf
s return 3 and the *
is the familiar multiplication operator, giving 9.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With