当前位置:首页 > 编程技术 > 正文

c语言中如何比较两个字符串

c语言中如何比较两个字符串

在C语言中,比较两个字符串通常使用`strcmp`函数,该函数定义在`string.h`头文件中。`strcmp`函数比较两个字符串直到遇到第一个不同的字符或字符串的结...

在C语言中,比较两个字符串通常使用`strcmp`函数,该函数定义在`string.h`头文件中。`strcmp`函数比较两个字符串直到遇到第一个不同的字符或字符串的结尾(null字符`0`)。

以下是使用`strcmp`函数比较两个字符串的示例代码:

```c

include

include

int main() {

char str1[] = "Hello";

char str2[] = "World";

char str3[] = "Hello";

// 比较str1和str2

int result = strcmp(str1, str2);

if (result < 0) {

printf("str1 is less than str2n");

最新文章