C-string and formatted input/output, c-string
1. A string is a sequence of one or more characters. String constants are enclosed by double quotation marks (abc), and character constants are enclosed by single quotation marks ''.
2. arrays are ordered sequences of data elements of the same type. Data elements are continuously stored in the memory.
C does not define a special variable type for the string, but stores it in the char array.
C string storage usually ends with a null character '\ 0. A null character is not a number 0, but a non-printable character with an ASCII code 0. It is used to mark the end of a string. The existence of null characters means that the char array length must be 1 more than the number of characters to store.
Char name [10]; // declares an array of 10 characters.
# Define NAME "gaotian"
Scanf ("% s", name );
Printf ("% s, % s", name, NAME );
3. After scanf () starts to read the input, it will stop reading at the first blank character (space, Tab \ t, line break \ n.
4. Differences between strings and characters
The character 'X' is the basic data type. The string "x" is a derived char array. "X" actually contains two characters: 'X' and '\ 0 '.
5. the sizeof () operator returns the size of data or type in bytes. The strlen () function returns the length of a string in characters. For the same String constant, sizeof will calculate '\ 0', because it also occupies the memory, and strlen won't.
6. character constants
When constants are required in the program, character constants should be used. One is high readability and the other is convenient to modify.
Four methods:
-Declare a variable and assign a value to the required constant.
Int age = 12;
-Pre-processing commands define symbol constants (# define symbol name constant value)
Note:
There is no semicolon at the end; not a C statement
Generally, all symbols are in uppercase.
During compilation, all content after the symbol name will be used to replace the place where the symbol name appears.
Constants defined in this way are called obvious constants.
# Define NAME "gaotian"
# Define AGE 12
-Const Modifier
Declare the variable as read-only. It can be read for display and calculation and cannot be modified.
Const int AGE = 12;
-Enum of Enumeration type
The 7. C header files limits. h and float. h provide detailed information about integer and floating point size restrictions. Defines a series of obvious constants.
8. printf ()-read, interpret, convert (convert numeric values to string text), and print
Printf (), scanf () is called an input/output function or an I/O function. Reciprocal operations.
Both printf () and scanf () have control strings (the former is the characters to be printed and the conversion specifiers; the format string (Conversion specifiers, common characters )) and parameter list (the former is a variable, constant, expression; the latter is a pointer to a variable ).
Printf () prints the string text, while scanf () receives the string text (that is, the keyboard inputs the string text ).
The type of the variable in the parameter list determines the data storage method. The conversion specifiers in the control string determine the Data Reading method and specify the data display form.
Note: The format specifiers must correspond to the number and type of variables one by one.
Conversion specifiers in printf:
% A, % A floating point number, hexadecimal number, p-Counting Method
% E, % E floating point number, e-Counting Method
% F floating point number, decimal notation
% G, % G automatically selects % f, % e, % E according to different values. % E and % E are used when the exponent is smaller than-4 and the accuracy is greater than or equal.
% C one character
% S string
% P pointer
% D, % I signed decimal integer
% U unsigned decimal integer
% O unsigned octal integer
% X, % X unsigned hexadecimal integer
% Print a %
Modifiers in printf ):
-Left alignment
+ If the signed value is positive, the + sign is displayed. If the value is negative, the-sign is displayed.
(Space) if the signed value is positive, leading spaces are displayed. If it is negative, A-sign is displayed. + the modifier overwrites the (Space) modifier.
# For the % o format, it starts with 0; For the % x, % X format, it starts with 0x, 0X; for all floating point formats, # ensure that no number is followed in real time, it also prints a decimal point character. For % g, % G, # To prevent trailing zeros from being deleted.
0 (0) for all numeric formats, use leading zero instead of spaces to fill the field width. If a-modifier or precision is specified, the modifier is ignored.
Minimum width of the digit (s) Field
. Digit (s) precision. For % e, % E, % f, it indicates the number of digits on the right of the decimal. % G, % G indicates the maximum number of digits that are valid. % S indicates the maximum length of the string. For integer format, it indicates the minimum number of digits. It is not enough to fill in the leading zero. Note: It is equivalent to. 0.
H and Integer Conversion specifiers are used together to indicate short int and unsigned short int
Hh and Integer Conversion specifiers are used together to indicate short char and unsigned short char
L and Integer Conversion specifiers are used together to represent long int, unsigned long int
Ll and Integer Conversion specifiers are used together to represent long int, unsigned long int
L and floating point conversion specifiers are used together to represent long double
Z and Integer Conversion specifiers are used together to represent a size_t value (type of the sizeof return value)
T and Integer Conversion specifiers are used together to represent a ptrdiff_t value (the type of the difference between the two pointers)
Note: There are conversion specifiers used to print the double and long double types, but there is no float type conversion specifier. Because, in C, the vertices are double by default, and the float type is automatically converted to double.
Printf () reads and interprets binary values in the computer based on the placeholder size and storage mode of the Data Type indicated by the conversion specifier, and converts them into a series of characters (strings) for display. The original value is not changed, but different representations are specified.
Functions in C generally return values. Is the value returned to the caller by function compute.
Printf () Return Value: Normal. Return the number of printed characters, including all printed characters, including spaces and line breaks. Error. A negative number is returned.
Printf () print a long string:
Blank characters (spaces, tabs, line breaks) are ignored if they are not used to separate elements.
Four Methods: carriage return between elements (strings enclosed in quotation marks cannot be broken); multiple printf () statements are used, with one containing '\ n' at most '; backslash \ + carriage return; String concatenation (two strings are separated by only blank characters, and C is combined into one string for processing)
9. scanf ()-read (read by character), convert (convert string text to numeric values), explain, and store
Scanf () reads the basic data type. The variable name is prefixed with an ampersand &;
Scanf () divides the input into multiple fields based on the blank characters, and matches the conversion specifiers and fields in turn.
Conversion specifiers in scanf:
% E, % E, % f, % F, % g, % G, prinft () for float and double, scanf () for float only.
% C is interpreted as a character
% S is interpreted as a string starting from the first non-blank character to the next white space character.
% D, % I is interpreted as a signed decimal number
% U unsigned decimal number
% O signed Octal numbers
% X, % X signed hexadecimal number
% P A pointer (address)
% A, % A floating point
% E, % E, % f, % F, % g, % G float floating point number (float only, l modifier is required for double, and L modifier is required for long double)
The conversion modifier of scanf:
* Lagging Value assignment: discard the corresponding read content
Digit (s) maximum field width. When it reaches the maximum field width or encounters a blank character, it stops reading the input item.
H % hd, % hi, indicates it will be stored in short int; % hu, % ho, % hx indicates it will be stored in unsigned short int;
Hh reads integers as signed char and unsigned char
L % ld, % li long; % lu, % lo, % lx unsigned long; % le, % lf, % lg double; % Le, % Lf, % Lg long double;
Ll reads integers as long and unsigned long
Without such modification, d, I, o, x indicates int type; e, f, g indicates float type.
Scanf () input principle:
The scanf conversion specifiers provide information about the data types to be read. Each Data Type represents the desired characters in a specific character set, for example, a % d pair should contain a signed decimal integer, the character set is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-, + }.
Scanf reads the input string one by one and skips the white space until the first non-white space character is met. If it is in a specific character set, read and save it and continue scanning the next character, until the first non-specific character set character is encountered, scanf stops reading and puts the last read non-specific character set character back into the input. Finally, scanf converts the string it reads into a numerical value and stores it in the specified variable. If the first non-blank character is a non-specific character set, scanf () stops there, and put the read characters back into the input. The next character is not read, and no value is assigned to the specified variable. The next scanf () will still be read from this character.
White space characters, non-specific character set characters, field width, read to the end of the input content are the conditions for scanf () to stop reading.
When scanf reads % s, it will add 'null character \ 0' at the end of the string, and then store it in the char array to make it a C string.
When scanf reads % c, all characters are read equally, including blank characters.
The format string of scanf () can contain common characters. Apart from space characters, your format string must exactly match the input string. For example, scanf (% d, % d); you must enter 12,345 or 12,345, followed by 12, followed by comma ',' no space. Spaces in the format string indicate that any spaces before the next input item are skipped. Scanf (% d, % d); you can enter 12,345; 12,345; 12,345;
Return Value of scanf (): return the number of successfully read items. If nothing is read, 0 is returned. If it is read to the end of the file, EOF is returned.
Conversion modifier *. In printf (), * can be used to indicate the variable field width. The corresponding variable is required in the parameter list. In scanf (), the corresponding items are discarded.