5
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?

7 Answers 7

7

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 printfs

The arguments to a function are evaluated before the function is called which is why the inner printfs 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)

Sign up to request clarification or add additional context in comments.

Comments

5

The order of the internal printfs 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 printfs return 3 and the * is the familiar multiplication operator, giving 9.

2 Comments

It is not undefined it is unspecified, big difference
Technically that's two words... :)
2

printf like other IO functions returns the number of bytes printed.

This is done so that you can check if IO went as expected, since file / stream IO errors can happen and sometimes the only way to know that is to check that the right number of bytes got printed.

Comments

2

printf returns the number of bytes written. So the "hi!" and "bye" return 3 each. 3*3 is 9 and that's what is printed

Comments

1

printf() returns the number of characters (bytes) it printed successfully. Please read the man page of printf().

Comments

1

You get hi!bye9 because printf returns the number of bytes output or a negative number if there is an error.

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

On success, the total number of characters written is returned. If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

So with:

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

First of all the two inner printf calls output hi! followed by bye. Then the return values from those two get interpreted similar to the below (they get multiplied together and output by the outer printf):

printf("%d", 3 * 3);

Comments

0

Note that

  1. printf is function of standard library and most of library functions return something.
  2. It returns the length of what you want to write inside the " ".
  3. If you have a space inside the printf function it will be add it in the length.

Example

#include<stdio.h>
void main()
{
     int x;
     x=printf("xyz \n");
     printf("%d",x);
     getch();
}

Here, the first printf statement executes then it returns the value of the first printf which is 4. Then we store this value in variable x and print the variable. So output will be:

xyz
4

Here value is 4 because we used a space

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.