C language basics-Lesson 4-arrays and strings, array strings
1 array 1.1 Definition and use of one-dimensional arrays
Int array [10]; // defines a one-dimensional array named array. There are 10 elements in total, each of which is of the int type. Array [0] = 20; Array [1] = 30; Array [9] = 80; // Array [10] = 100; // error, no array [10] |
1.2 Storage of arrays in memory
An array is a continuous space in the memory. Each element has the same type.
1.3 initialize a one-dimensional array
Int array [10] = {100, 1, 5, 3, 4, 5, 6, 7, 8, 0}; // defines the array and initializes the value for the array members. |
Int array [10] = {3, 7, 9}; // assign values to the first three elements of the array, and set the remaining elements to 0. |
Int array [10] = {0}; // set all elements of the array to 0 |
Int I; For (I = 0; I <10; I ++) { Array [I] = 0; // cyclically traverse each element of the array and set the element value to 0. } |
1.4 definition and use of two-dimensional arrays
Int array [2] [3]; // defines a two-dimensional array with two arrays [3] |
1.5 two-dimensional array Initialization
Int a [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }}; |
|
2 string and character array 2.1 character array Definition
Initialize an array of 2.2 characters
Char array [100] = {'A', 'B', 'C', 'D '}; |
Char array [100] = "abcd "; |
Char array [100] = {0 }; |
Char array [] = "abcd "; |
The 2.3 character array uses a 2.4 random number to generate the functions rand and srand.
Header file stdlib. h
Rand is a pseudo-random number generator. Each call to rand produces the same random number.
If you call srand before calling rand, any random number will appear.
As long as the parameter values are different each time the srand function is called, the rand function will produce different random numbers.
# Include <time. h> Int t = (int) time (NULL ); Srand (t ); For (int I = 0; I <10; I ++) { Printf ("% d \ n", rand ()); } |
|
2.5 use scanf to input a string
Char s [10] = {0 }; Scanf ("% s", s); // "% s" is used to input a string, and scanf is marked by the Enter key, however, the Enter key itself is not part of the string. // If the array length in the scanf parameter is smaller than the length entered by the user on the keyboard, scanf will overflow the buffer and cause program crash Int I; For (I = 0; I <10; I ++) { Printf ("% d \ n", s [I]); } Printf ("---------------------------------- \ n "); Printf ("% s \ n", s ); Return 0; |
2.6 string end flag
Scanf returns the carriage return and spaces are considered as the end mark of string input,
2.7 string processing function 2.7.1 gets
Char s [100] = {0 }; Gets (s); // gets indicates that the end mark of the input of the carriage return, and the space is not the end mark of the Input. Therefore, you can use the gets function to input a string with spaces. // There is a buffer overflow problem between gets and scanf. Int I; For (I = 0; I <10; I ++) { Printf ("% d \ n", s [I]); } Printf ("---------------------------------- \ n "); Printf ("% s \ n", s ); |
Gets cannot be escaped using characters such as "% s" or "% d". It can only accept string input.
2.7.2 fgets Function
The gets function does not check whether the reserved buffer can accommodate the user's actual input data. The extra characters will cause memory overflow, which is improved by the fgets function.
Because the fgets function is designed to read files, reading the keyboard is not as convenient as gets.
Char s [100] = {0 }; Fgets (s, sizeof (s), stdin ); |
2.7.3 puts Function
The puts function prints a string. Unlike printf, puts automatically adds '\ n' at the end'
Char s [] = "hello world "; Puts (s ); |
2.7.4 fputs Function
Fputs is the file operation version of puts,
Char s [] = "hello world "; Fputs (s, stdout ); |
2.7.5 strlen, String Length
Size_t strlen (const char * _ Str );
Returns the length of a string that does not contain the string ending with '\ 0'.
Char s [100] = "hello world "; Int len = strlen (s); // get the string length, returns the number of valid characters in a string (excluding 0 at the end of the string) Printf ("len = % d \ n", len ); Return 0; |
2.7.6 strcat, string appending
Size_t strcat (char * _ Str1, const char * _ Str2 );
Append parameter _ Str2 to the end of _ Str1
Char s [1024] = "hello world "; Int len = strlen (s); // get the string length, returns the number of valid characters in a string (excluding 0 at the end of the string) Printf ("len = % d \ n", len ); Char s1 [100] = "abc123456789 "; Strcat (s, s1); // merge the two strings and put the results into the first parameter. strcat also has the buffer overflow problem. Printf ("% s \ n", s ); |
2.7.7 strncat, limited string appending
Size_t strncat (char * _ Str1, const char * _ Str2, size_t len );
2.7.8 strcmp, string comparison
Int strcmp (const char * _ Str1, const char * _ Str2 );
Returns 0 if the two strings are equal, and returns non-0 if the values are not equal.
2.7.9 strncmp, character string limited comparison 2.7.10 strcpy string copy
Char * strcpy (char * _ Str1, const char * _ Str2 );
Copy parameter _ Str2 to parameter _ Str1
2.7.11 strncpy character string limited copy 2.7.12 sprintf, Format String
Similar to the printf function, the printf function outputs the formatted result to the screen, and sprintf outputs the formatted result to the string.
2.7.13 Sscanf Function
Sscanf is similar to the scanf function. scanf reads user input from the keyboard and scanf reads the input from the specified formatted string.
2.7.14 strchr lookup characters
Char * strchr (char * _ Str, int _ Ch );
In parameter _ str, search for the specified parameter _ Ch and find the position of the returned character _ Ch in _ Str. If no value is found, return NULL;
2.7.15 strstr
Char * strstr (char * _ Str, const char * _ SubStr)
Search for parameter _ SubStr in parameter _ str to indicate the stator string. Find the position of the returned substring in _ Str. If no value is found, return NULL;
2.7.16 split string with strtok
When the character is called for the first time, strtok () must be given the parameter s string. In future calls, the parameter s is set to NULL. If each call is successful, a pointer pointing to the split part is returned.
Char buf [] = "abc @ defg @ igk "; Char * p = strtok (buf ,"@"); While (p) { Printf ("% s \ n", p ); P = strtok (NULL ,"@"); } |
2.7.17 convert atoi to int
The header file stdlib. h must be included.
2.7.18 convert atof to float2.7.19 atol to long