(i) strcmp function
The strcmp function compares the size of two strings and returns the result of a comparison. The general form is:
I=STRCMP (String, string);
where string 1, string 2 can be either a string constant or a variable, and I is an integer variable that holds the result of the comparison. The result of the comparison is this:
① string 1 is less than the string 2,strcmp function returns a negative value;
② string 1 equals string 2,strcmp function returns 0;
③ string 1 is greater than the string 2,strcmp function returns a positive value, so how is the size of the characters compared? Take a look at an example.
In fact, the comparison of strings is the ASCII code that compares the pairs of characters in a string. The first character of the two string is compared first, and if it is not equal, the comparison is stopped and the result is greater or less than the second character, then the third character, and so on. If the characters in front of the string are always equal, like "Disk" and "Disks", the first four characters are the same, then compare the fifth character, the previous string "Disk" only left the Terminator '/0 ', after a string "disks" remaining ' s ', '/0 ' ASCII code is less than The ASCII code of ' s ', so the result is obtained. So no matter what the two strings are, the strcmp function compares up to one of the strings and encounters the Terminator '/0 ' so that the result can be obtained.
Note: The string is an array type, not a simple type, and cannot be compared using relational operations for size comparisons.
if ("ABC" > "DEF")/*/Wrong string comparison */
if (strcmp ("ABC", "DEF")/* Correct string comparison */
(ii) strcpy function
The strcpy function is used to implement a copy of two strings. The general form is:
strcpy (character 1, string 2)
Where string 1 must be a string variable, not a string constant. The strcpy function completely copies the contents of string 2 into string 1, regardless of what was originally stored in string 1. After copying, the string 2 remains unchanged.
Note that because the string is an array type, two string copies do not pass the assignment operation.
T=s; /* Wrong string copy */
strcpy (t,s); /* Correct string copy */
strcmp functions and strcpy functions of common functions in C language