[英]Why does 0.0 == 0 means true in C language and when condition is 0.0 means false in branching statement?
[英]Why does the nested if statement output true when there is no condition requirement?
我正在努力處理嵌套的“if 語句”,我不知道為什么在沒有條件要求時它顯示為 true,而當我更改返回值時它顯示為 false。 另外為什么當我將函數的原始類型從 int 更改為 char 時沒有區別?
#include <stdio.h>
#include <strings.h>
int random(char z[]);
int main() {
char *x ="Tup";
char *y ="Nope";
printf("%s\n", x);
printf("%d\n", random("Hi"));
if(random("random char")){
if(x){
printf("True");
}
}else if(y){
printf("False");
}
return 0;
}
int random(char z[])
{
char a[3] ="yak";
return a[0] == 'y';
}
random
函數返回條件a[0] == 'y'
。 由於您將a
定義為char a[3] ="yak"
這始終是正確的。
因此, if (random("random char"))
為真,因此進入if
部分。 然后評估if(x)
。 x
指向字符串常量"Tup"
,意味着它有一個非零值,導致條件為真。 所以“True”然后被打印出來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.