关于C程序if括号里为什么只是一个变量

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

main()
{
int i;
int hasUpper, hasLower, hasDigit;
char user[25], password[25];

hasUpper = hasLower = hasDigit = 0;

printf("用户名");
scanf(" %s", user);

printf("密码");
scanf(" %s",password);

for (i = 0; i < strlen(password); i++)
{
if (isdigit(password[i]))
{
hasDigit = 1;
continue;
}
if (isupper(password[i]))
{
hasUpper = 1;
continue;
}
if (islower(password[i]))
{
hasLower = 1;
}
}

if ((hasDigit ) && (hasLower ) && (hasUpper ))
{
printf("你输入的密码正确\n");
printf(" %d,%d,%d.", hasUpper, hasLower, hasDigit);
}
else
{
printf("你输入的密码错误\n");
printf(" %d,%d,%d.", hasUpper, hasLower, hasDigit);
}

return 0;

}

我是一个新手,想知道,为什么if后面是三个变量,不是三个判断式,还有上面三个初始化变量为0 是什么意思?

if (hasDigit) 其实就相当于 if (hasDigit != 0),也就是说判断hasDigit是否为非0,非0时成立,0时不成立追问

那如果我不为1的话是不是必须写成 hasDigit != 1,能简略写吗?

追答

不为1就必须写成 hasDigit != 1

if (hasDigit) 只能判断是否非0

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网