请考虑以下代码:
char foo[32] = "123456";
printf("strlen(foo) = %d\n", strlen(foo));
if ((5 - strlen(foo)) > 0)
{
//This statement prints because the comparison above returns true, why?
printf("String must be less than 5 characters, test 1\n");
}
int tmp;
if ((tmp = 5-strlen(foo)) > 0)
{
//This statement does not print and makes since
printf("String must be less than 5 characters, test 2\n");
}
Run Code Online (Sandbox Code Playgroud)
正如评论所示,我不明白为什么在与另一个值工作进行比较之前需要一个临时变量来存储数学计算的结果.