
在linux命令行下,man strcmp,可以看到函数说明:
$ man strcmp...
NAME
strcmp, strncmp - compare two strings
SYNOPSIS
#include <string.h>
int strcmp(const char *s1, const char *s2)
DESCRIPTION
The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
strcmp函数比较两个字符串s1和s2. 函数返回小于0,等于0,或大于0的整数,分别对应s1<s2, s1=s2, s1<s2 .
比如 s1 = "good" s2="...this is good", 从s2取后四位进行两个字符串比较,代码如下:
#include <stdio.h>#include <string.h>
int main()
{
char *s1 = "good" , *s2="...this is good"
int len=strlen(s2)
if ( len >= 4 ) //如果串长超过4位,则移动指针到最后四位的位置
s2 =s2+len-4
printf("s2=%s\n", s2 ) //输出移位后的字符串内容
printf("compare s1,s2=%d\n", strcmp(s1,s2) ) //输出0,表示相同
return 0
}
1、计算机用两个字节来表示一个汉字,“我”在内存里就是这样存放的:CED2。CE是str[0]的内容,D2是str[1]的内容。第一次循环输出str[0],但是这个字符在ASCII字符集里代表这样一个东西(不知道在这儿能不能正常显示),但是Windows的命令提示符程序读取到这里就会自动使用宽字符集,也就是说,它已经准备好读取下一个字符,然后把他们当成一个字符显示出来。于是就出现了那个汉字。
2、例程:
#include <stdio.h>
int main()
{
printf("%c%c",(char)0xce,(char)0xd2)
}
c语言字符串的结束符是 '\0' 也就是ASCII 码 0不知道你注意到没有,我没有加 Linux,是因为这时 C 语言规定,跟 *** 作系统无关,在 Windows 下也是一样
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)