Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf related Query

Tags:

c

printf

Here is a simple one line program using printf :

void main()

{
printf("%d%d",printf("Cis"),printf("good"));
}

Output :

goodCis34

How can this output be explained ??

like image 844
Leo Messi Avatar asked Nov 21 '11 15:11

Leo Messi


People also ask

What is printf() in C?

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.

How does printf work?

The printf function (the name comes from “print formatted”) prints a string on the screen using a “format string” that includes the instructions to mix several strings and produce the final string to be printed on the screen.

What is a in printf?

The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases. As an example, this code: printf("pi=%a\n", 3.14);

How does printf know how many arguments?

The printf function uses its first argument to determine how many arguments will follow and of what types they are. If you don't use enough arguments or if they are of the wrong type than printf will get confuses, with as a result wrong answers.


1 Answers

The reason why good and Cis are printed first is because the parameters need to be evaluated before the top-level printf() can be called.

Then the return values are printed out.

Note that C does not specify the order of evaluation of the parameters. There are no sequence points within the statement. Therefore the order is undefined. And the result can appear in any order. (hence why they appear to be evaluated out-of-order in this case)

like image 133
Mysticial Avatar answered Sep 28 '22 12:09

Mysticial