strcmp functions and strcpy functions of common functions in C language

Source: Internet
Author: User
Tags strcmp

(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.

Cases:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.