Why does “hello” evaluate as true in a boolean condition?

你离开我真会死。 提交于 2020-01-06 11:04:27

问题


I have been doing work in C language as it was my first language from where i started.This time i was doing some coding with if else statement...and i find something which i cannot understand why it was happening.The code is as follow

#include<stdio.h>
#include<conio.h>

void main()
{
    printf("How If - Else works\n");

    if("hello")
    {
        printf("Inside if\n");
    }
    else
    {
        printf("Inside else\n");
    }

    getch();
} 

In this the output was Inside if....i want to know how this thing get evaluated inside the if statement and is there any restriction to what we pass in the if statement.


回答1:


C doesn't really have boolean (true or false) values (C99 does, but my notes below still apply).

What C interprets as false is anything "0"; everything else is true;

so

if (0) {} else {printf("0 is false\n");}
if (NULL) {} else {printf("NULL is false\n");}
if (0.0) {} else {printf("0.0 is false\n");}

a literal string is interpreted as a pointer ... and it is pointing to real characters, so it's true

if (1) {printf("1 is true\n");} else {}
if (-1) {printf("-1 is true\n");} else {}
if ("hello") {printf("\"hello\" is true\n");} else {}
if (3.14159) {printf("3.14159 is true\n");} else {}

Interestingly an empty string or the string "0" or the character '0' is true

if ("") {printf("\"\" is true\n");} else {}
if ("0") {printf("\"0\" is true\n");} else {}
if ('0') {printf("'0' is true\n");} else {}

The NUL character (not NULL which is a pointer) has int value 0 and is false

if ('\0') {} else {printf("'\\0' is false\n");}

What happens when you have a real boolean construct is that the compiler emits code to convert that to 0 or 1

if (a > b) /* whatever */;
// if a is greater than b, the compiler generated code will be something like
if (1) /* whatever */;
// otherwise, if a <= b, the generated code would look like
if (0) /* whatever */;



回答2:


The (expression) will evaluate to true if it can be converted to bool. "hello" is a string literal which evaluates to a pointer and since it's different than 0 it will result in true. A null pointer would return false.

The statement is valid if the expression can be implicitly converted to bool and this happens with most expressions: all scalar types (chars, arithmetic types, pointers, enums, etc) and also struct/class variables if they have an operator returning a scalar type. Functions returning no value at all (void) would generate an error.




回答3:


If statement takes care of true and false only. So, if it's not false, everything else is a true.

False could be a 0, FALSE (bool), NULL. All the expressions get evaluated to one of true or false.

In your case, it is evaluates to true because it is not one of the false expressions.




回答4:


C doesn't have boolean datatype. As such, the statements requiring truth values accept that a value equal to 0 represents false and any other value represents true. NULL also evaluates to false (it is a macro defined as 0).

In your code "hello" is a non-null (different from 0) pointer to a string. This way, it will always be true inside if statements.

Anything that has a value can be used as an expression for this kind of statement.




回答5:


A "string literal" in C represents a sequence of characters. This evaluates to a value other than zero or NULL; so the control flows into the if-loop.

Any independent expression can be evaluated inside the if conditional parentheses with the semntics as stated by the following in the C standard:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value

So, the flow of the program goes into the else part if you do something like:

if(1,1,1,0) {
    printf("Inside if\n");
}

since the result of the expression takes the value of the last operand (0).




回答6:


Strings equate to a value True. Hence, your program flow goes into the if body and not the else body.



来源:https://stackoverflow.com/questions/22362283/why-does-hello-evaluate-as-true-in-a-boolean-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!