#include<stdio.h>
#include <string.h>
void main(){
char name[16] , pwd[16];
int a , b;
printf("请输入用户名:");
fgets(name,sizeof(name),stdin);
printf("请输入密码:");
fgets(pwd,sizeof(pwd),stdin);
printf("%s\n%s" ,&name , &pwd);
a = strcmp(name,"abc");
b = strcmp(pwd,"123456");
printf("%d\n",&a);
printf("%d",&b);
if(strcmp(name,"abc") == 0 && strcmp(pwd,"123456") == 0){
printf("---交换机管理系统---\n");
printf("1.登录\n");
printf("2.创建帐号\n");
printf("3.退出\n");
}else{
printf("\n用户名或者密码不正确!\n");
}
}
得到的不是0或者1 或者-1
首先,你的代码里printf用错了,不需要加取地址符&, 如printf("%d\n",&a); 应该是 printf("%d\n",a);
其次,如@空无 所说,strcmp(str1,str2)中字符串相等时是0,str1<str2返回值<0,str1>str2时返回值>0, 具体取值为str1/str2中第一个不相同的位置两个字符的差值,如"ap"比"ar"为-2, "ap"比"an"为2。
strcmp用法可参见:http://www.cplusplus.com/refe...