What is the reasoning behind types to be redefined in GLib? Why do they turn char
into gchar
, int
into gint
, etc.?
Asked
Active
Viewed 1,599 times
14

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 Answers
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 typedef
ed just to make all of the type names look uniform.
-
1
-
4In 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
-
9I 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