"Most programming languages, even those that do not have an explicit
Boolean type, have support for Boolean algebra operations such as
conjunction (AND, &, *), disjunction (OR, |, +), equivalence (EQV, =,
==), exclusive or/non-equivalence (XOR, NEQV, ^), and not (NOT, ~, !)."
C
To this day, Boolean values are commonly represented by integers in C
programs. The comparison operators (' > ', '==', etc.) are defined to
return a signed integer (int) result, either zero (for false) or
nonzero (for true). The Boolean operators (&&, ||) and conditional
statements (if, while) in C operate on integer values, with the same
interpretation. For example, the following C code
int t = (x > y);
if (t) { printf("True!\n");}
else { printf("False!\n"); }
is equivalent to
int t;
if (x > y) { t = -1; }
else { t = 0; }
if (t != 0) { printf("True!\n"); }
else { printf("False!\n"); }
However, since the C language standards allow the result of a
comparison to be any non-zero value, the statement if(t==1){...} is not
equivalent to if(t){...} or to if(t!=0){...}.
Since C standards mandate that 0 be interpreted as the null pointer
when used in a pointer context or cast to a pointer, one can test
whether a pointer is non-null by the statement if(p){...} instead of
if(p!=NULL){...} — although some code styles discourage this shorthand.
One can also write if(x){...} when x is a floating-point value, to mean
if(x!=){...}.
For convenience, many programmers and C header files use C's typedef
facility to define a Boolean type (which may be named Boolean, boolean,
bool, etc.). The Boolean type may be just an alias for a numeric type
like int or char. In that case, programmers often define also macros
for the true and false values. For example,
typedef int bool;
#define FALSE 0
#define TRUE (-1)
...
bool f = FALSE;
...
if (f) { ... }
The defined values of the TRUE and FALSE macros must be adequate for
the chosen Boolean type. Note that, on the now common two's complement
computer architectures, the signed value -1 is converted to a non-zero
value (~0, the bit-wise complement of zero) when cast to an unsigned
type, or assigned to an unsigned variable.
Another common choice is to define the Boolean type as an enumerated
type (enum) allows naming elements in the language of choice. For
example, the following code uses the English names FALSE and TRUE as
possible values of the type boolean. In this case, care must be taken
so that the false value is represented internally as a zero integer:
typedef enum { FALSE, TRUE } boolean;
...
boolean b = FALSE;
...
if (b) { ... }
Again, since a true result may be any non-zero value, the tests
if(t==TRUE){...} and if(t), which are equivalent in other languages,
are not equivalent in C.
-http://en.wikipedia.org/wiki/Boolean_datatype#C
Boolean type, have support for Boolean algebra operations such as
conjunction (AND, &, *), disjunction (OR, |, +), equivalence (EQV, =,
==), exclusive or/non-equivalence (XOR, NEQV, ^), and not (NOT, ~, !)."
C
To this day, Boolean values are commonly represented by integers in C
programs. The comparison operators (' > ', '==', etc.) are defined to
return a signed integer (int) result, either zero (for false) or
nonzero (for true). The Boolean operators (&&, ||) and conditional
statements (if, while) in C operate on integer values, with the same
interpretation. For example, the following C code
int t = (x > y);
if (t) { printf("True!\n");}
else { printf("False!\n"); }
is equivalent to
int t;
if (x > y) { t = -1; }
else { t = 0; }
if (t != 0) { printf("True!\n"); }
else { printf("False!\n"); }
However, since the C language standards allow the result of a
comparison to be any non-zero value, the statement if(t==1){...} is not
equivalent to if(t){...} or to if(t!=0){...}.
Since C standards mandate that 0 be interpreted as the null pointer
when used in a pointer context or cast to a pointer, one can test
whether a pointer is non-null by the statement if(p){...} instead of
if(p!=NULL){...} — although some code styles discourage this shorthand.
One can also write if(x){...} when x is a floating-point value, to mean
if(x!=){...}.
For convenience, many programmers and C header files use C's typedef
facility to define a Boolean type (which may be named Boolean, boolean,
bool, etc.). The Boolean type may be just an alias for a numeric type
like int or char. In that case, programmers often define also macros
for the true and false values. For example,
typedef int bool;
#define FALSE 0
#define TRUE (-1)
...
bool f = FALSE;
...
if (f) { ... }
The defined values of the TRUE and FALSE macros must be adequate for
the chosen Boolean type. Note that, on the now common two's complement
computer architectures, the signed value -1 is converted to a non-zero
value (~0, the bit-wise complement of zero) when cast to an unsigned
type, or assigned to an unsigned variable.
Another common choice is to define the Boolean type as an enumerated
type (enum) allows naming elements in the language of choice. For
example, the following code uses the English names FALSE and TRUE as
possible values of the type boolean. In this case, care must be taken
so that the false value is represented internally as a zero integer:
typedef enum { FALSE, TRUE } boolean;
...
boolean b = FALSE;
...
if (b) { ... }
Again, since a true result may be any non-zero value, the tests
if(t==TRUE){...} and if(t), which are equivalent in other languages,
are not equivalent in C.
-http://en.wikipedia.org/wiki/Boolean_datatype#C
No comments:
Post a Comment