printf("strcmp=0x%bx\n", strcmp("123", "123"));
printf("strcmp=0x%bx\n", strcmp("1234", "123"));
printf("strcmp=0x%bx\n", strcmp("123", "1234"));
printf("strcmp=0x%bx\n", strcmp("1233", "1234"));
这几个函数,打印出来的结果,都是strcmp=1
真是日了狗了,这是为啥?
离线
楼主的代码用了%bx,应该是Keil C51编译的吧?
我在本地编译并仿真了一下,结果是正确的。测试代码如下:#include <stdio.h> #include <string.h> sfr SCON = 0x98; sbit TI = SCON^1; void main(void) { TI = 1; printf("strcmp=0x%bx\n", strcmp("123", "123")); printf("strcmp=0x%bx\n", strcmp("1234", "123")); printf("strcmp=0x%bx\n", strcmp("123", "1234")); printf("strcmp=0x%bx\n", strcmp("1233", "1234")); for (;;); }
输出结果如下:
strcmp=0x0 strcmp=0x1 strcmp=0xff strcmp=0xff
楼主把反汇编的结果放上来看看?
软件仿真的结果是对的,download到芯片里面跑,就不正确了。
使用strncmp,就没有问题。像下面这样写:
printf("strncmp=0x%bx\n", strncmp("123", "123", 3));
printf("strncmp=0x%bx\n", strncmp("1234", "123", 4));
printf("strncmp=0x%bx\n", strncmp("123", "1234", 4));
printf("strncmp=0x%bx\n", strncmp("1233", "1234", 4));
离线