14

What is the reasoning behind types to be redefined in GLib? Why do they turn char into gchar, int into gint, etc.?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Lucas
  • 13,679
  • 13
  • 62
  • 94
  • See also: [Why is it better to use Glib data types (e.g. `gint` instead of `int`)?](https://stackoverflow.com/a/13821850/4561887) – Gabriel Staples Dec 01 '22 at 00:29

2 Answers2

13

Check out Basic Types in the GLib documentation. Essentially, it's to guarantee that certain types will exist with certain semantics, regardless of which C compiler or platform you're using. The types that C guarantees anyway are typedefed just to make all of the type names look uniform.

nemequ
  • 16,623
  • 1
  • 43
  • 62
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 1
    So the reason to turn char into gchar is to make it look prettier? – Lucas Nov 30 '09 at 12:41
  • 4
    In a way, but mainly because int for example may not be the same on all systems, gint8 will always be 8 bits however. – Dave Nov 30 '09 at 13:08
  • 9
    I guess. Some of the gtypes (arguably) give extra semantics over a simple type, but I think the rest are just for consistency and portability. Also remember that GLib dates from 1999, predating the widespread availability of types like `uint64_t`, which is why they roll their own. – Josh Lee Nov 30 '09 at 13:08
0

See also this answer.

Essentially, it's because glib's standard-width types such as guint32 predate the C99 and later standard-width types, such as uint32_t, which now exists (as of C99 and later) and is exactly equivalent.

So, glib's g-prefixed types are now essentially obsolete. Use the standardized fixed-width types like uint32_t now instead.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265