Can anyone please exlain this to me:
if(something else)
{
somebool = d >= 0.3 * (a + 1)
}
Where "somebool" is of type bool and is initially set to true and "a"
of type int. I don't understand in C how you can appear to assign a
boolean a numeric value.
Can anyone please help me? Would appreciate any comments/suggestions/
code-simplifications.
Section 9 of the comp.lang.c FAQ, <
http://www.c-faq.com/>, has some
good material on boolean expressions in C.
It would help if you could explain where the type "bool" is coming from.
C90 has no boolean type. It's fairly common to declare your own
boolean type; a typical definition might be:
typedef enum { false, true } bool;
or
typedef int bool;
#define false 0
#define true 1
Or to use names other than "bool", "false", and "true". Relational
operators yield a result of type int, with the value 0 or 1; this can
easily be converted to whatever boolean type you've defined. But
assigning a value other than 0 or 1 can give you a result that's not
equal to either true or false -- though any non-zero value is treated
as true in a conditional context.
C99 has a built-in type _Bool, and a standard header <stdbool.h> that
declares macros "bool", "false", and "true". (The latter aren't
built-in because they can conflict with pre-C99 code). But equality
and relational operators still yield results of type int. Converting
any scalar value to _Bool yields either 0 or 1 (false or true); the
conversion normalizes all non-zero values to 1.
Finally, C++ has a built-in type bool, and C++'s equality and
relational operators yield results of type bool (if I recall
correctly). Note that this is different from either C90 or C99. If
you're using a C++ compiler, you should ask in comp.lang.c++.