Page 1 of 1

How to declare boolean on SGDK?

Posted: Wed Oct 03, 2012 3:13 am
by ehs03y3ol
Source code part of SDK types.h:

Code: Select all

/**
 *  \def FALSE
 *      FALSE define (equivalent to 0).
 */
#ifndef FALSE
#define FALSE   0
#endif
/**
 *  \def TRUE
 *      TRUE define (equivalent to 1).
 */
#ifndef TRUE
#define TRUE    1
#endif
/**
 *  \def NULL
 *      NULL define (equivalent to 0).
 */
#ifndef NULL
#define NULL    0
#endif
Code that crashes:

Code: Select all

bool beta;
Code that output:

Code: Select all

main.c||In function `main':|
main.c|17|error: `bool' undeclared (first use in this function)|
main.c|17|error: (Each undeclared identifier is reported only once|
main.c|17|error: for each function it appears in.)|
main.c|17|error: syntax error before "beta"|
||=== Build finished: 4 errors, 1 warnings ===|

Posted: Wed Oct 03, 2012 3:22 am
by ehs03y3ol
okey, i reply myself

Nota: here's no boolea in C -- instead use char, int or best unsigned char

Posted: Wed Oct 03, 2012 8:17 am
by Stef
Exactly, no bool in C ;)
Just a general hint, generally on 68000 the u16/s16 type is the fastest so except if memory is a concern just use it, even for bool test.

Posted: Wed Oct 03, 2012 1:51 pm
by slobu
I don't use C but isn't this a thing to be handled by constants?

Something like:

0 = none (state has not been defined)
1 = true
2 = false

Making null the same value as false (0) sounds like it could lead to bugs.

Posted: Wed Oct 03, 2012 2:07 pm
by Stef
slobu wrote:I don't use C but isn't this a thing to be handled by constants?

Something like:

0 = none (state has not been defined)
1 = true
2 = false

Making null the same value as false (0) sounds like it could lead to bugs.
A boolean do not have undetermined state, it should be true or false. And in C it's well known that FALSE is 0 and TRUE is not 0 :)

Posted: Wed Oct 03, 2012 4:14 pm
by GManiac
Stef wrote:And in C it's well known that FALSE is 0 and TRUE is not 0 :)
AFAIR, C defines TRUE as 1. But with 68k it's better to use -1 (#$FF, 255). Why? Because value -1 is invariant in logical and bitwise operators:
not( 0 ) = -1
not( FALSE ) = TRUE

68k has even special instructions Scc which set byte to 0 or to -1. They are useful for calculating logical expressions.

Posted: Wed Oct 03, 2012 5:59 pm
by Chilly Willy
Booleans tend to be one of the most non-portable aspects of C that contribute to bugs. I've seen programs that assume TRUE is 1 or that TRUE is -1 and use logical operations in arithmetic operations assuming one or the other it correct. Never directly use the result of a logical operation in an expression! For example:

bad

Code: Select all

    x = coord * (object_state == ACTIVE);
good

Code: Select all

    x = (object_state == ACTIVE) ? coord : 0;