Josh Morrison
Josh Morrison

Reputation: 7636

gnu C condition of "if"

we got if(expression) {...} for example. We all know if expression is true, it will execute lines in braces. But what is "True" in C? Is that != 0 means true as I think?

Thank you

Upvotes: 0

Views: 271

Answers (3)

Sergey Shandar
Sergey Shandar

Reputation: 2387

Yes, true is not-null in C and C++.

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96109

Any none-zero result tests as true.

Upvotes: 3

dreamlax
dreamlax

Reputation: 95335

Here is what the standard has to say.

§6.8.4 Selection statements

Syntax

  1. selection-statement:
    if ( expression ) statement
    if ( expression ) statement else statement
    switch ( expression ) statement

§6.8.4.1 The if statement

Constraints

  1. The controlling expression of an if statement shall have scalar type.

Semantics

  1. In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.
  2. An else is associated with the lexically nearest preceding if that is allowed by the syntax.

Upvotes: 7

Related Questions