19 Mar 2005 TazForEver   » (Journeyer)

The gboolean trap

gboolean is just int !

#include <stdbool.h>
#include <stdio.h>

#include <glib.h>

int main() { float f; gboolean gb; bool b;

f = 0.5f; gb = f; b = f;

printf("gboolean %d\n" "bool %d\n", (int)gb, (int)b);

return 0; }

Ouput :

gboolean 0
bool     1

0.5f is converted to int 0. But 0.5f is obsviously not zero and therefore should be evaluated as TRUE. So be carefull with gboolean.

NB: <stdbool.h>, bool, true and false are C99.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!