0X04 string and formatted input/output, 0x04 string
A character string is a sequence of one or more characters. For example, "hello world", double quotation marks are not part of a string, but are used to notify the compiler. This is a string.
C does not define a special variable type for a string, but stores it in a char array. The number of units in the array must be 1 more than the number of characters, because the string will have \ 0 at the last position of the array.
In printf, % s is used to print a string.
The strlen () function, in characters, returns the length of a string. You need to use the string. h header file, which contains many function prototypes related to the string.
Sizeof: whether to use parentheses depends on whether to obtain the type size or the specific amount size, such as: sizeof (int), sizeof name or sizeof 13
Const: This keyword converts a variable declaration to a constant declaration.
Printf () and scanf ()
I/O function, that is, input/output function ). At first, it is not a function defined by C, but the implementation of input/output is left to the user, which can better match I/O with a specific machine.
Printf
Conversion specifier
Conversion description |
Output |
% |
Floating Point Number, hexadecimal number, and p-counting (C99) |
% |
Floating Point Number, hexadecimal number, and p-counting (C99) |
% C |
One character |
% D |
Signed decimal number |
% E |
Floating Point Number and e-Counting Method |
% E |
Floating Point Number and E-Counting Method |
% F |
Floating Point Number and decimal notation |
% G |
% F or % e is automatically selected based on different values. % Egex used when the index is smaller than-4, and the live cover is equal to the precision |
% G |
% F or % E is automatically selected based on different values. % Egex used when the index is smaller than-4, and the live cover is equal to the precision |
% I |
Signed decimal INTEGER (same as % d) |
% O |
Unsigned octal integer |
% P |
Pointer |
% S |
String |
% U |
Unsigned decimal integer |
% X |
Use a hexadecimal number 0f and an unsigned hexadecimal integer |
% X |
Use a hexadecimal number 0f and an unsigned hexadecimal integer |
% |
Print a percent sign |
Modifier
Modifier |
Meaning |
Flag |
Five logos (-, +, space, #, and 0) Example: %-19d |
Digit (s) |
The minimum value of the field width. If the field cannot contain the number or string to be printed, the system uses a wider field. Example: % 4d |
. Digit (s) |
Precision Example: % 3.2f: print a floating point number. Its field width is 3 and there are two digits after the decimal point. |
H |
Used Together with Integer Conversion instructions to indicate a short int or unsigned short int type value. Example: % hu % hx % 6.4 h |
Hh |
Used Together with Integer Conversion instructions to indicate a signed char or unsigned char type value Example: % hhu |
J |
It is used with the Integer Conversion specifier to indicate an intmax_t or uintmax_t value. Example: % jd % 8jd |
L |
It is used together with the Integer Conversion instruction to indicate a long int or unsigned long int type value. Example: % ld |
Ll |
Used Together with Integer Conversion instructions to indicate a long int or unsigned long int type value (C99) Example: % lld |
L |
It is used with the floating point conversion specifier to indicate a long double value. Example: % Lf |
T |
Used together with the Integer Conversion specifier to indicate a ptrdiff_t value (the value corresponding to the difference between the two pointers) (C99) Example: % td % 12ti |
Z |
Used together with the Integer Conversion specifier to indicate a size_t value (type returned by sizeof) (C99) Example: % zd |
Flag |
Meaning |
- |
Project is left aligned |
+ |
If the signed value is positive, the plus sign is displayed. If the value is negative, the minus sign is displayed. |
(Space) |
If the signed value is positive, leading spaces (but not symbols) are displayed. If the value is negative, a minus sign is used. + The flag will overwrite the space flag. |
# |
Optional Form of conversion instructions |
0 |
For All numeric formats, use leading zero instead of spaces to fill the field width. If a-flag appears or an integer is specified, the flag is ignored. |
Scanf
The conversion specifiers and modifiers are mostly the same as those of printf.
The return value of scanf () is the number of successfully read projects. When it detects that "End of File" is, it returns EOF (EOF is a special value defined in stdio. h, generally-1)
* Modifier of printf and scanf
If you do not want to specify the field width in advance, but want the program to specify this value, you can use * to replace the number in the field width section.