日志9

strcmp是C语言中的一个字符串比较函数,用于比较两个字符串。它的原型如下:

int strcmp(const char *str1, const char *str2);
参数:
str1:第一个要比较的字符串。
str2:第二个要比较的字符串。
返回值:
如果str1和str2相等,返回0。
如果str1小于str2,返回负数。
如果str1大于str2,返回正数。

示例代码:
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "hello";
    char str2[] = "world";
    char str3[] = "hello";

    int result;

    result = strcmp(str1, str2);
    if (result == 0) {
        printf("str1 and str2 are equal.
");
    } else if (result < 0) {
        printf("str1 is less than str2.
");
    } else {
        printf("str1 is greater than str2.
");
    }

    result = strcmp(str1, str3);
    if (result == 0) {
        printf("str1 and str3 are equal.
");
    } else if (result < 0) {
        printf("str1 is less than str3.
");
    } else {
        printf("str1 is greater than str3.
");
    }

    return 0;
}
输出结果:
str1 is less than str2.
str1 is equal to str3.
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 12:18
点赞 评论 收藏
分享
点赞 评论 收藏
分享
吴offer选手:下午mt一来就告警说项目来不及,估计明天拿了权限就要参与开发了 已老实
实习生的蛐蛐区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务