Recommended usage of GLib types in GTK programming?

I’ve noticed that older documentation recommends using GLib types over standard C types in most occasions, but newer documentation and examples seem to omit these types. Is the current recommendation to not use these GLib types or is something else occurring under the covers? Thanks.

Can you share an example ?

For your own code you should C99 types, except for gboolean, which is not the same as C99’s _Bool.

2 Likes

An example would be using gint (a GLib type) instead of int.

Thanks. Just out of curiosity, what changed for the recommendation to be standard C99 types over GLib ones?

GLib types were introduced when C89/C90 was the only C standard, so it was missing a lot of types we take for granted. Additionally, there were a lot more fringe architectures at the time, with weirdly sized types in their C standard library and toolchain. On the other hand, types that exactly match C types were introduced because the original creators of GLib/GTK wanted to have consistent syntax highlighting for them in Emacs for all type declarations.

Since C99 introduced sized integers, and since MSVC started supporting C99 more appropriately, it’s less important to have GLib-specific types; and the “consistent syntax highlighting” excuse is definitely not valid any more.

The only exception is gboolean, which is a strict alias for int, whereas _Bool in C99 is defined to be big enough to store a char, but it has no defined size. This means that _Bool and gboolean are not binary compatible when used inside a structure. You can use gboolean and _Bool interchangeably at the function boundary level, though.

5 Likes

Great explanation. Thanks.

1 Like

Nice to know. Thanks for the great explanation.

Also, I think this should go into some glib doc page.

Thanks.

Not really, no. I mean: we can’t remove those types, so they have to stay there; and various libraries still use them. Whether you want to use them or not it’s entirely up to you.

For newly written code, a minimum of C99 is recommended; but, again, it depends on the type of toolchain you’re targeting.

1 Like

I think this historical explanation and usage recommendation is certainly appropriate for the official documentation.

But updating documentation is work. :wink:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.