c语言strcmp函数用法_strcmp()C库函数用法和示例

c语言strcmp函数用法

c语言strcmp函数用法

C programming standard library provides strcmp() function in order to compare two strings and return the results whether they are identical or different.

C编程标准库提供strcmp()函数,以便比较两个字符串并返回相同或不同的结果。

语法和参数 (Syntax and Parameters)

As stated previously strcmp() function takes two char array or string arguments.

如前所述,strcmp()函数采用两个char数组或字符串参数。

int strcmp (const char* str1, const char* str2);
  • const char* str1 is the first string or char array which will be compared the second one. const is mainly used to prevent given char array pointer to be changed.

    const char * str1是第一个字符串或char数组,将与第二个字符串或char数组进行比较。 const主要用于防止更改给定的char数组指针。
  • const char* str2 is the second string or char array which will be compared with the first one.

    const char * str2是第二个字符串或char数组,将与第一个比较。

返回值 (Return Values)

strcmp() function returns an int or integer type. We can get 3 types of return value that are explained below.

strcmp()函数返回一个int或整数类型。 我们可以得到以下三种返回值类型。

  • `0` is returned if both strings are identical, equal or the same.

    如果两个字符串相同,相等或相同,则返回“ 0”。
  • `Negative Integer` if the ASCII value of the first unmatched character is less then second

    “负整数”,如果第一个不匹配字符的ASCII值小于第二个字符
  • `Positive Integer` if the ASCII value of the first unmatched character is greater than second

    如果第一个不匹配字符的ASCII值大于第二个,则为“正整数”

比较两个字符串 (Compare Two Strings)

We can compare two strings which are expressed as char array in C programming language. We will compare strings “I love poftut.com” and “I loves poftut.com” .

我们可以比较两个用C编程语言表示为char数组的字符串。 我们将比较字符串“我爱poftut.com”和“我爱poftut.com”。

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "I love poftut.com", str2[] = "I loves poftut.com";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}

We will compile with gcc like below and then run the binary.

我们将使用下面的gcc进行编译,然后运行二进制文件。

$ gcc strcmp.c -o strcmp
$ ./strcmp
Compare Two Strings
Compare Two Strings
比较两个字符串

比较两个字符数组(Compare Two Char Array)

We can compare two char arrays where

我们可以比较两个字符数组

#include <stdio.h>
#include <string.h>

int main()
{
  char str1[] = "I love poftut.com", str2[] = "I love poftut.com";
  int result;

  // comparing strings str1 and str2
  result = strcmp(str1, str2);
  printf("strcmp(str1, str2) = %d\n", result);

return 0;
}
LEARN MORE  strstr() Function in C and C++ Tutorial with Examples
在C和C ++教程中了解更多strstr()函数的示例
Compare Two Char Array
Compare Two Char Array
比较两个字符数组

We can see that both of the character arrays are the same so the return value will be 0 and printed to the screen.

我们可以看到两个字符数组都相同,因此返回值将为0并打印到屏幕上。

Compare Two Char Array
Compare Two Char Array
比较两个字符数组

字符串不同,第一个更大,并返回正值(Strings Are Different and First Is Bigger and Return Positive Value)

In this example, the first string is bigger and return a positive integer which is the value of the character.

在此示例中,第一个字符串较大,并返回一个正整数,该整数是字符的值。

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", str2[] = "aBcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}
Strings Are Different and First Is Bigger and Return Positive Value
Strings Are Different and First Is Bigger and Return Positive Value
字符串不同,第一个更大,并返回正值

字符串不同,第二个更大,返回负值(Strings Are Different and Second Is Bigger Return Negative Value)

In this example, the second string is bigger and return a positive integer which is the value of the character.

在此示例中,第二个字符串更大,并返回一个正整数,该整数是字符的值。

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "aBcd", str2[] = "abcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);
}

Strings Are Different and Second Is Bigger Return Negative Value
Strings Are Different and Second Is Bigger Return Negative Value
字符串不同,第二个更大,返回负值

字符串相同返回0(Strings Are Same Return 0)

If both strings are the same the strcmp() function will return zero 0 .

如果两个字符串相同,则strcmp()函数将返回零0。

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", str2[] = "abcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}
Strings Are Same Return 0
Strings Are Same Return 0
字符串相同返回0

翻译自: https://www.poftut.com/strcmp-c-library-function-usage-with-examples/

c语言strcmp函数用法

  • 14
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值