#include < stdio.h > int main() { int i = 20,j; i = ( printf ( "Hello" ), printf ( " All Geeks " )); printf ( "%d" , i); return 0; } |
The printf() function returns the number of characters it has successfully printed. The comma operator evaluates it operands from left to right and returns the value returned by the rightmost expression.First printf(“Hello”) executes and prints “Hello”, the printf(” All Geeks “) executes and prints ” All Geeks “. This printf statement returns 11 which is assigned to i.
How to print % using printf()?
Here is the standard prototype of printf function in C.
int printf(const char *format, ...);
The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of argument (and it is an error if insufficiently many arguments are given).The character % is followed by one of the following characters.
The flag character
The field width
The precision
The length modifier
The conversion specifier:
See http://swoolley.org/man.cgi/3/printf for details of all the above characters. The main thing to note in the standard is the below line about conversion specifier.
A `%' is written. No argument is converted. The complete conversion specification is`%%'.
So we can print “%” using “%%”/* Program to print %*/ #include<stdio.h> /* Program to print %*/ int main() { printf ( "%%" ); getchar (); return 0; } |
printf ( "%c" , '%' ); printf ( "%s" , "%" );
Question 2:
Output: “GEEKS % FOR % GEEKS” Backslash (\) works as escape character for double quote (“).
Explanation: Standard printf function definition int printf ( const char * format, ... );format: String that contains the text to be written to stdout. It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested. The number of arguments following the format parameters should at least be as much as the number of format tags. The format tags follow this prototype: %[flags][width][.precision][length]specifierYou can see details of all above parts here http://www.cplusplus.com/reference/clibrary/cstdio/printf/. The main thing to note is below the line about precision * (star): The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. So, in the above example 5 is precision and 10 is the actual value to be printed. Evaluation of arguments in printf int main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Output:45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack and the evaluation is from right to left, hence the result. |
Evaluation of arguments in printf is incorrect.
ReplyDeleteThe code you use has a variable being modified multiple times within a single sequence point and the results of the operation are 'undefined' by the spec. Running this code on other platforms/compilers may result in different values being printed.
The comma in the function call is NOT the same as the comma operator, and function arguments are _defined_ to be evaluated in 'any order the implementation chooses'.
@Shelby: You are right in saying the output is undefined or compiler dependent; but What i am trying to emphasize is if you have got the written output then the explanation would be using stacks as given.If you will be asked to come up with an output then you are right.
Delete