返回首页   进站必读

8.7 strcmp/strncmp


8.7 strcmp/strncmp

比较字符串

#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
返回值:负值表示s1小于s2,0表示s1等于s2,正值表示s1大于s2

举例:

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

int main(int argc, char *argv[])
{
	char *str = "hello";
	char *arr = "hoho";
    
	printf("%d\n", strcmp(str, arr));

	return 0;
}

打印出来的是-1,说明字符串s1小于字符串s2