Definition and use of one or one-D arrays
In the 1.C language, the subscript of an array element is starting from 0
2. When defining an array, you need to set its length, which must be an integer constant
Note: Constants take advantage of macro definitions: replacing Long data with short identifiers facilitates program modifications and upgrades replace hard-to-remember data with easy-to-understand identifiers
Format: #define PI 3.14 (no semicolon)
3. If you assign an initial value to all array elements, you may not specify the length of the array
When initializing, if the number of initial values is less than the number of elements, the remaining auto-complement 0
Two, character array and string
1. Strings are usually stored in a character array, with each element holding one character and ending with '/'
2. String initialization: char str[] = "Hello";
Error initialization: Char str[20];
STR = "Hello"; String constants are read-only
Functions in the 3.<string.h>:
(1) Puts function: output a string
(2) Gets function: Enter a string and save it in a character array
Difference from scanf: scanf input until a white space character is entered until the line break is encountered
Note: The empty buffer statement should be added between two inputs: Fflush (stdin);
(3) Strcat function: String connection prototype: strcat (Dest, SRC);
(4) strcpy function: String copy prototype: strcpy (Dest, SRC);
(5) strcmp function: string comparison str1 <str2 returns a negative integer
STR1 = = str2 returns 0 comparison mode: From left to right, in turn, compare ASCII
STR1 > STR2 Returns a positive integer
(6) Strlen function: Calculates the length of a string (excluding '% ')
4.SCANF input string should be careful not to write &
scanf ("%s", str); The character array name itself is already an address
5.SCANF Supplemental Instructions:
(1)%[^ ...] Indicates a read to the end of a particular content encounter
(2)%[...] Indicates that only specific content is read
Example: scanf ("%[^\n]", str); Enter Hello world! Enter Str:hello world!
scanf ("%s", str); Enter Hello world! Enter Str:hello
scanf ("%[12345678]", str) input 1234hello5678 return str:1234
(3) The control character "%-4d" means that the output is left aligned
C Language Learning notes-6. Arrays