The usage of strcmp and strncmp

Source: Internet
Author: User
Tags strcmp

strcmp and strncmp are used to compare strings, and the difference is whether the specified length string can be compared. strcmp

C + + functions, comparing two strings
Set these two strings as STR1,STR2,
If STR1==STR2, return 0;
If str1 > str2, return a positive number;
If str1< str2, a negative number is returned.

That is: two strings are compared from left to right by character (compared to the size of the ASCII value) until there are different characters or "".

Such as:
The return value of strcmp ("ABCD", "ABCD") is 0;
The return value of strcmp ("ABCD", "DCBA") is-1;
The return value of strcmp ("DCBA", "ABCD") is 1;

There is also a situation:
The return value of strcmp ("A", "C") is-2;
The return value of strcmp ("C", "A") is 2;
This exact value is dependent on the implementation of the different C

Special NOTE: strcmp (const char s1,const char s2) which can only compare strings, can be used to compare two string constants, or compare array and string constants, not other forms of parameters such as numbers. strncmp

The STRNCMP function specifies the size character of the comparison. In other words, if the string S1 is the same as the first size character of the S2, the function returns a value of 0. This function compares the first maxlen characters of the string str1 and str2. If the first maxlen byte is exactly equal, the return value is = 0; in the first maxlen byte comparison, if STR1[N] differs from Str2[n, then the first n bits of str1 and str2 are compared in turn, and the I (i< N) is the first different bit of the two strings, then returns (STR1 [I]-str2[i]).

As
Str1= "Abcdhg", str2= "ABCDEF"
The return value of strncmp (str1,str2,4) is 0;
The return value of strncmp (str1,str2,5) is 1;

Description: Compare the size of string str1 and str2, if the str1 is less than str2, the return value is <0, if the str1 is greater than str2, the return value is >0, if the str1 equals STR2, the return value = 0,len refers to the number of characters that are compared to str1 and str2. This function compares the first Len characters of the string str1 and str2 [1].
Tip: This function is case-sensitive.

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.