Explain this nested expression printf

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 conclusion occurred. Does this mean that the printf function returns a value? What is the reason for running internal printf commands?

+4
source share
6 answers

The internal order is printfnot defined. Another implementation, the same implementation with different compiler settings, or the same implementation with the same code elsewhere can result in byehi!9.

It returns 9 because it printfreturns the number of characters printed, so the two internal returns printf3 and *is the familiar multiplication operator, giving 9.

+4
source

, , , :

printf("%d",printf("hi!")*printf("bye"));
            ^             ^
            1             2

, 1 2, , . C99 draft standard 6.5 , 3, ( ):

.74) , ( -(), &, ||,?: ), , , .

, printf , -1, , , , 3 printf s

, , printf, 6.5.2.2 , 4:

. , , .81)

+5

printf, -, .

, , IO , , - /, , , .

+2

printf . , "!" "bye" 3 . 3 * 3 - 9

+2

printf() (), . , printf().

+1

hi!bye9, printf , .

http://www.cplusplus.com/reference/cstdio/printf/:

. , (ferror) .

,

printf("%d",printf("hi!")*printf("bye"));

First of all, two internal calls printfare deduced hi!, and then bye. Then, the return values ​​from these two are interpreted similarly to the following (they are multiplied together and output externally printf):

printf("%d", 3 * 3);
+1
source

All Articles