Next: Logical operators
Up: Types
Previous: Two-dimensional arrays
Contents
Index
Logical expressions
There is no boolean type in C (you cannot declare a variable to be
boolean, or return a boolean from a function), but it does have a
notion of a logical expression usually found in if, for and
while statements.
|
if (a == 1) {
/* .. */
}
while (row < n_rows) {
/* .. */
}
|
Such expressions are normally formed using one of the comparison
operators (<, <=, == (equality) != (non-equality),
>= and >). In fact, if e is an arbitrary expression
which is to be interpreted as true of false, C will treat the expression as
e!=0 if e is a numeric type
|
if (a) {
/* do something */
}
|
will be interpreted as
|
if (a != 0) {
/* so something */
}
|
C will convert from a logical expression to a numerical type
automatically using the convention: true becomes 1 (or 1.0 for
floating-point types), false becomes 0 (or 0.0). Combined with the
behavior described above, you can use any numerical type as if it were
boolean, with the interpretation zero=false, non-zero=true.
|
int a; /* we'll use this as if it were boolean */
a = (1<2); /* assigns 1 to a because the expression is true */
|
(CCATSL defines a boolean type and boolean constants
true and false.)
Subsections
Next: Logical operators
Up: Types
Previous: Two-dimensional arrays
Contents
Index
CATAM admin
2010-02-23