Find the answer to your Linux question:
Results 1 to 4 of 4
I guess I'm just a noob, but I notice that some C libraries (and even some C++ ones) tend to redefine the basic types with their own naming convention. For ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    8

    Why redefine basic types in C?

    I guess I'm just a noob, but I notice that some C libraries (and even some C++ ones) tend to redefine the basic types with their own naming convention.

    For example, OpenGL redefines the basic types:

    Code:
    from GL/gl.h
    typedef unsigned int GLenum;
    typedef unsigned char GLboolean;
    typedef unsigned int GLbitfield;
    typedef signed char GLbyte;
    typedef short GLshort;
    typedef int GLint;
    etc...
    Is there some reason for this? Why use GLint instead of int?
    I've also seen some code that does this:

    Code:
    #ifdef _MSC_VER
            typedef unsigned __int32        u32;
    #else
    	typedef unsigned int            u32;
    #endif
    #ifdef _MSC_VER
            typedef __int32                 s32;
    #else
    	typedef signed int              s32;
    #endif
    Is there some difference on windows between __int32 and int?
    Thanks again and excuse me for being a noob.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    The reason is that OpenGL is supposed to be pretty portable. So, for example, a GLint should have the same size no matter what machine the code is compiled on.

    How big, say, the datatype int is is determined by the vendor of the compiler. It may be 16bit on old computers, or 128bit somewhere in the future.

    By having the datatypes wrapped, OpenGL code will be easier to port.

    The same holds for:
    Code:
    #ifdef _MSC_VER
            typedef unsigned __int32        u32;
    #else
    	typedef unsigned int            u32;
    #endif
    If _MSC_VER is defined, the datatype __int32 is know by the compiler and chosen. If not, the compiler is believed to be old enough so that the "ordinary" int is of size 4 (byte) = 32bit.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Yes, although it should be noted that the fixed sizes of int8_t, uint8_t, int16_t and so on from the stdint.h header, any other use of such types for the purposes of keeping code portable is redundant, since using the standard types is obviously more portable. They were useful before stdint.h was actually in the C standard, though.

  4. #4
    Just Joined!
    Join Date
    May 2009
    Posts
    8
    Quote Originally Posted by GNU-Fan
    How big, say, the datatype int is is determined by the vendor of the compiler. It may be 16bit on old computers, or 128bit somewhere in the future
    Did not know that. Thanks for that information!


    Quote Originally Posted by JohnGraham
    Yes, although it should be noted that the fixed sizes of int8_t, uint8_t, int16_t and so on from the stdint.h header, any other use of such types for the purposes of keeping code portable is redundant, since using the standard types is obviously more portable. They were useful before stdint.h was actually in the C standard, though.
    Thanks, thats good to know. From now on, I will use those.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...