1.char array type and null character
C does not define a specific variable type for a string, but instead stores it in a char array. The last position of the array displays the character. This character is a null character, C is used to mark the end of the string, and its ASCII value is (or equal to) 0. C's string storage usually ends with this null character, which means that the number of cells in the array must be at least 1 more than the number of characters to store. The computer can handle most of these details on its own (for example, scanf () adds '% ' to make the contents of the array a C string).
2.strlen () function and sizeof operator
The sizeof operator gives the size of the data in bytes, and the strlen () function gives the length of the string in characters.
#include <stdio.h> #include <string.h> /* Provides prototypes of the strlen () function */#define PRAISE "What a Super Marvelous name!" int main (void) {char name[40]; printf ("What ' s Your name?\n"); scanf ("%s", name); printf ("Hello,%s,%s\n", name, praise); printf ("Your name of%d letters occupies%d memory cells.\n", strlen (name), sizeof (name)); printf ("The phrase has%d letters", strlen (Praise)); printf ("and occuples%d memory cells. \ n", sizeof (praise)); return 0;}
Output Result:
What ' s your name?
Morgan Buttercup
Hello, Morgan. What a super marvelous name;
Your name of 6 letters occupies memory cells.
The phrase of praise has letters and occupies memory cells.
According to the report of the sizeof operator, the array name has 40 memory units. But only the first 6 units were used to store Morgan, as reported by Strlen (). The seventh cell of the array name places a null character, and its presence tells Strlen () where to stop counting.
For praise, you will find that strlen () once again gives the exact number of characters (including spaces and punctuation) in the string. The sizeof operator gives you a large number of 1, because it calculates the invisible null character that is used to end the flag string. You do not tell the computer how much memory is allocated to store the statement, and it must calculate the number of characters between the double quotes itself.
3. Constants
1) The preprocessor allows you to define constants by simply adding the following information at the top of the program file:
#define TaxRate 0.015
When compiling your program, the value 0.015 will be substituted for each place where TaxRate appears.
2) Use the Const keyword to convert a variable declaration into a constant declaration:
const int mouths = 12;
4.printf () and scanf ()
The printf () function also has a return value that returns the number of characters printed;
The return value of the scanf is determined by the following parameters:
scanf ("%d%d", &a, &b);
If both A and B are read successfully, then the return value of scanf is 2.
If only a is successfully read in, the return value is 1
If both A and B are not successfully read, the return value is 0
If an error is encountered or an end of file is encountered, the return value is EOF.
And the return value is of type int.
If you use scanf () to read a value of a variable type, precede the variable name with &.
If you use scanf () to read a string into a character array, do not precede the variable name with &.
To print%, you should use percent.
Whitespace characters include spaces, tabs, and line breaks. C Separates individual language symbols with whitespace characters: scanf () separates adjacent entries with white space characters.
format conversion specifier printf ()/scanf ()
Table One conversion description indicators as the result of the printout
Conversion instructions |
Output |
%a |
Floating-point, hexadecimal-digit, and P-notation (C99) |
%A |
Floating-point, hexadecimal-digit, and P-notation (C99) |
%c |
A character |
%d |
Signed decimal integer |
%e |
Floating-point number, E-notation method |
%E |
Floating-point number, E-notation method |
%f |
Floating-point number, decimal notation |
%g |
Automatically selects%f or%e depending on the value. The%e format is used when the exponent is less than-4 or greater than or equal to precision |
%G |
Automatically selects%f or%e depending on the value. The%e format is used when the exponent is less than-4 or greater than or equal to precision |
%i |
Signed decimal integer (same as%d) |
%o |
unsigned octal integer |
%p |
Pointer (means address) |
%s |
String |
%u |
unsigned decimal integers |
%x |
Unsigned hexadecimal integer with hexadecimal digit 0f |
%x |
Unsigned hexadecimal integer with hexadecimal digit 0F |
%% |
Print a percent semicolon |
Table two printf () modifier
Modifier |
Significance |
Sign |
The five flags (-, +, spaces, #, and 0) are described in Table III and can be used with 0 or more flags |
Digit (s) |
The minimum value of the field width. If the field does not hold the number or string you want to print, the system uses a wider field. Example: "%4d" |
. Digit (s) |
Precision. For%e,%e, and%f conversions, is the number of digits that will be printed on the right side of the decimal point. For%g and%g conversions, the maximum number of digits for a valid number. For the%s conversion, is the maximum number of characters that will be printed. For integer conversions, the minimum number of digits to be printed, or, if necessary, the number of digits to be reached with the leading zero. Use only "." Followed by a 0, so the%.F is the same as%.0f. Example: "%5.2f" prints a floating-point number with a field width of 5 characters and two digits after a decimal point. |
H |
is used with the integer conversion specifier to represent a short int or unsigned short int type value. Example: "%hu", "%hx", and "%6.4HD" |
hh |
Used with integer conversion specifiers to represent a signed char or unsigned char type value. Example: "%hhu", "%HHX", and "%6.4HHD" |
J |
is used with the integer conversion specifier to represent a intmax_t or uintmax_t value. Example: "%jd" and "%8JX" |
L |
Used with an integer specifier, indicating a long int or unsigned long int value. Example: "%ld" and "%8lu" |
ll |
Used with an integer specifier, which represents a long long int or unsigned long long int (C99). Example: "%lld" and "%8llu" |
L |
is used with the floating-point conversion specifier to represent a long double value. Example: "%lf" and "%10.4le" |
T |
Used with integer conversion specifiers to represent a ptrdiff_t value (the type corresponding to the difference between two pointers) (C99) Example: "%td" and "%12ti" |
Z |
is used with the integer conversion specifier to represent a size_t value (the type returned by sizeof ) (C99). Example: "%zd" and "%12zx" |
Table Three The marks of printf ()
modifier |
|
- |
Project is left-aligned, that is, the project will be printed to the left of the field at the beginning. Example: "%-20s" |
+ |
Signed value if positive, A symbol with a plus sign is displayed, or a sign with a minus sign if it is negative. Example: "%+6.2f" |
(space) |
If the signed value is positive, the display is preceded by a leading space (but not the symbol) and, if negative, with a minus sign. The + flag will overwrite the space mark. Example: "% 6.2f" |
# |
Optional form with conversion description 。 In the case of the%o format, start at 0, and if the format is%x and%x, start at 0x or 0X, and for all floating-point forms, #保证了即使不限任何数字, and a decimal character is printed. For%g and%g formats, it prevents trailing 0 from being deleted. Example: "% #o", "% #8.0f" and "%+ #10.3E" |
0 |
For all number formats, fill the field width with leading zeros instead of spaces. If the-flag is present or the precision is specified (for integers), the flag is ignored. Example: "%010d" and "%08.3f" |
Table four conversion specifiers for scanf () in Ansic
Conversion specifier |
Significance |
%c |
Interpret the input as a character |
%d |
Interpret the input as a signed decimal integer |
%e,%f,%g,%a |
Interpret the input as a floating-point number (%a is the C99 standard) |
%e,%f,%g,%a |
Interpret the input as a floating-point number (%a is the C99 standard) |
%i |
Interpret the input as a signed decimal integer |
%o |
Interpret the input as a signed octal number |
%p |
Interpret the input as a pointer (address) |
%s |
Interprets the input as a string; The input begins with a non-whitespace character and contains all the characters that know the next whitespace character |
%u |
Interprets the input as an unsigned decimal integer |
%x,%x |
Interpret the input as a signed hexadecimal integer |
Table five conversion modifiers for scanf ()
Modifier |
Significance |
* |
Lag assignment. Example: "%*d" |
Digit (s) |
The maximum width of the field, or when the first blank character is encountered (whichever occurs first) stops reading the entry. Example: "%10s" |
hh |
Reads an integer as signed char or unsigned char. Example: "%HHD" "%hhu" |
ll |
Reads an integer as long long or unsigned long long (C99). Example: "%lld" "%llu" |
H,l or L |
"%hd" and "HI" indicate that the value will be stored in a short int. "%ho" "%hx" and "%hu" indicate that the value will be stored in a unsigned short int. "%ld" and "%li" indicate that the value will be stored in a long. "%lo" "%lx" and "%lu" indicate that the value will be stored in a unsigned long. "%le" "%lf" and "%LG" indicate that the value is stored as a double type. Use L (not L) with E, F, and G to indicate that the value is stored in a long double type. Without these modifiers, D, I, O, and x indicate the int type, while E, F, and G indicate the float type. |
C Language Learning (ii)--string and formatted input and output