Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explain this nested printf statement

Tags:

c

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 output has occurred. Does this mean printf function returns a value?

What is the reason behind inner printf statements being executed first?

like image 429
itsme.cvk Avatar asked Jan 06 '14 14:01

itsme.cvk


People also ask

What is printf ()? Explain?

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.

Can we use printf statement inside printf?

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.

What is the purpose of printf () and scanf () in C program?

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).

What is formatted output using printf () statement explain it?

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.


2 Answers

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)

like image 195
Shafik Yaghmour Avatar answered Sep 28 '22 17:09

Shafik Yaghmour


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.

like image 22
Jack Aidley Avatar answered Sep 28 '22 16:09

Jack Aidley