scanf
- % [flag] type
1. [flag] (format part)
flag | meaning |
---|---|
* | jump over |
number | Maximum number of characters |
hh | char |
h | short |
l | long,long |
ll | long long |
L | long double |
1. Skip (*)
scanf("%*d%d%d",&a,&b,&c);
printf("%d+%d\n",a,b);
printf("%d+%d+%d\n",a,b,c);
Enter the statement on the first line, add * in the middle of %d corresponding to a. From the corresponding output, we can know that the number stored in the address of a is 22, and the number stored in the address of b is 33. The value stored in the address of c is the value allocated by the system.
I won’t explain the others one by one. The exercises we usually do are enough to see its usage at a glance. Some examples are also put together with printf.
printf
- % [ flags ] [width] [.prec] [hIL] type
1. [flag] (format part)
flag | meaning |
---|---|
- | align left |
+ | put + or - in front |
(space) | Positive numbers leave blank |
0 | 0 fill |
1. Left alignment (-)
printf("%9d\n",123);
printf("%-9d\n",123);
The running result is shown in the figure below. Both the first and second lines of output have nine spaces, but the second line is left-aligned.
2. Put + or - (+) in front
printf("%+9d\n",123);///强制输出加号
printf("%-+9d\n",123);
3. Positive numbers are left blank (space)
This is literally.
printf("%9d\n",123);
4. 0 padding (0)
printf("%09d\n",123);///0用于填充
printf("%-9d\n",123);
2. [width] or [.prec] (precision part)
width or prec | meaning |
---|---|
number | minimum number of characters |
* | The next parameter is the number of characters |
.number | number of digits after the decimal point |
.* | The next parameter is the number of digits after the decimal point |
1、 number / .number
printf("%9.2f\n",123.0);
"9" in the output statement indicates that the entire output occupies nine positions, and ".2" indicates that two digits are reserved after the decimal point.
2. *or.*
printf("%*d\n",6,123);
printf("%.*f\n",4,123.0);
The first line statement: '6' corresponds to '*', indicating that the output occupies 6 positions as a whole.
The second line statement: '4' corresponds to '.', which means there are 4 positions after the decimal point.
3. [hlL] (modified part)
type modification | meaning |
---|---|
hh | single byte |
h | short |
l | long |
ll | long long |
L | long double |
1、 hh
printf("%hhd \n",12345);
Here we only talk about the "hh" part, because the others have been mastered and used proficiently when they were beginners.
The conversion of 123456 in the output statement to hexadecimal is 0x3039, and only 39 is converted to 57 in decimal.
Fourth, type (type)
directly jumps to an article I wrote before, which includes almost all types, such as C language input and output .