1.3 Basic Types


1.3 Basic Types

To get started with GLib, you should adjust to its elementary types. You might wonder why it is important to use guchar instead of unsigned char . There aren't any real differences as long as you stay on the same platform. However, if you decide that you want to import, export, and interface your software between, say, Windows and Unix, then you'll be thankful that GLib can abstract the basic data types for you.

For example, if you want to do something unpleasant such as define an unsigned integer variable that is exactly 16 bits wide on any potential platform, things can start to look a little ugly in C. Fortunately, GLib takes care of this so that you don't have to get your hands too dirty. The basic types are listed in the table on the opposite page.

To use GLib and all of its types, include the glib.h header file in your source code:

 #include <glib.h> 

The gpointer and gconstpointer types appear frequently when interacting with the GLib data structures, because they are untyped pointers to memory. In GLib, functions that use these pointers take responsibility for verifying the type, not the programmer or the compiler. These can be especially handy for type abstraction in callback functions and equality operators used in sorting and iteration.

The GLib header file defines the constants TRUE and FALSE for the gboolean type. However, it's bad style to use equivalence operators with these constants; that is, use if (my_gboolean) , not if (my_gboolean == TRUE) .

GLib Type

Corresponding Type in C

gchar

char

ugchar

unsigned char

gint

int

guint

unsigned int

gshort

short

gushort

unsigned short

glong

long

gulong

unsigned long

gfloat

float

gdouble

double

gint8

int , 8 bits wide

guint8

unsigned int , 8 bits wide

gint16

int , 16 bits wide

guint16

unsigned int , 16 bits wide

gint32

int , 32 bits wide

guint32

unsigned int , 32 bits wide

gint64

int , 64 bits wide

guint64

unsigned int , 64 bits wide

gpointer

void * , untyped pointer

gconstpointer

const void * , constant untyped pointer

gboolean

Boolean value, either TRUE or FALSE




The Official GNOME 2 Developers Guide
The Official GNOME 2 Developers Guide
ISBN: 1593270305
EAN: 2147483647
Year: 2004
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net