printf and scanf key letters
The most important key letters are d, g (usually used as "lg"), and s.
| code | type | format |
|---|---|---|
| d | int | decimal (base ten) number |
| o | int | octal number (no leading '0' supplied in printf) |
| x or X | int | hexadecimal number (no leading '0x' supplied in printf; accepted if present in scanf) (for printf, 'X' makes it use upper case for the digits ABCDEF) |
| ld | long | decimal number ('l' can also be applied to any of the above to change the type from 'int' to 'long') |
| u | unsigned | decimal number |
| lu | unsigned long | decimal number |
| c | char [footnote] | single character |
| s | char pointer | string |
| f | float [footnote] | number with six digits of precision |
| g | float [footnote] | number with up to six digits of precision |
| e | float [footnote] | number with up to six digits of precision, scientific notation |
| lf | double [footnote] | number with six digits of precision |
| lg | double [footnote] | number with up to six digits of precision |
| le | double [footnote] | number with up to six digits of precision, scientific notation |
printf format modifiers
Modifiers appear between the '%' and the key letter.
- a number is a field width
- '.' and a number is a "precision"
- 0 (the digit zero) means pad with zeroes to field width (usually used only with integers)
- l (the letter) means "long", e.g. %ld to format a long int in decimal
Example of '0':
int dollars = 2;
int cents = 3; /* $2.03 */
printf(" ?? what goes here ?? ", dollars, cents);
| If format is | then output is |
|---|---|
| "$%d.%d" | $2.3 |
| "$%d.%2d" | $2. 3 (one space between "." and "3") |
| "$%d.%02d" | $2.03 |

本文详细介绍了C语言中printf和scanf函数的使用方法,包括格式字符串中的关键字母所对应的变量类型和格式,以及如何通过修饰符调整输出样式。
1080

被折叠的 条评论
为什么被折叠?



