How to declare boolean on SGDK?

SGDK only sub forum

Moderator: Stef

Post Reply
ehs03y3ol
Newbie
Posts: 2
Joined: Wed Oct 03, 2012 3:07 am

How to declare boolean on SGDK?

Post by ehs03y3ol » Wed Oct 03, 2012 3:13 am

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 ===|

ehs03y3ol
Newbie
Posts: 2
Joined: Wed Oct 03, 2012 3:07 am

Post by ehs03y3ol » Wed Oct 03, 2012 3:22 am

okey, i reply myself

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

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Wed Oct 03, 2012 8:17 am

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.

slobu
Very interested
Posts: 85
Joined: Tue Apr 03, 2012 6:02 pm

Post by slobu » Wed Oct 03, 2012 1:51 pm

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.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Wed Oct 03, 2012 2:07 pm

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 :)

GManiac
Very interested
Posts: 92
Joined: Thu Jan 29, 2009 2:05 am
Location: Russia

Post by GManiac » Wed Oct 03, 2012 4:14 pm

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.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Wed Oct 03, 2012 5:59 pm

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;

Post Reply