I could successfully execute the block of code below, however, could not understand the output of the code logically.
#include<stdio.h>
int main()
{
int x,y;
float f;
char text[22];
scanf("%d %2d %f %5s", &x, &y, &f, text);
printf("%d %d %f %s", x,y, f, text);
}
Input: 1 3456 5.6 web
Output: 1 34 56.000000 5.6
Query:
The code says:
With an input of 1 3456 5.6 web
, that is done as
The output should hence be (values with intermixed blanks from foramt string, using double quotes to visualise the different parts): "1"" ""34"" ""56.000000"" "" 5.6 "
.
Notes:
The best way to understand it is to visualize it IMO.
So when it prompts you for input you enter 1 3456 5.6 web
. Just imagine that stdin
looks like this:
1 3456 5.6 web
The first format specifier is %d
which just takes an integer and stores it into x
. After %d
:
3456 5.6 web
x = 1, y = ?, f = ?, text = ?
Next format specifier is %2d
, which means "take an integer, but the max digits will be 2, any digits after will be left untouched." So after %2d
:
56 5.6 web
x = 1, y = 34, f = ?, text = ?
Next is %f
. Pretty self-explanatory, takes a float
as input. However, since 56
is still left in stdin
, it takes 56
as the input instead of 5.6
. So after %f
:
5.6 web
x = 1, y = 34, f = 56.0f, text = ?
Next is the last, %5s
. It means "take a string, but take no more than 5 characters from input." However, it won't read web
. Since " 5.6 "
is still left before web
, it'll take " 5.6 "
(exactly 5 characters) instead as a string. So after %5s
:
web
x = 1, y = 34, f = 56.0f, text = " 5.6 "
And web
stays stuck in stdin
, untouched. I think you get the rest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.