Thread: typedef and portability

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    typedef and portability

    Generally we see a typedef for different data types

    Example:
    typedef unsigned char UINT8_T;
    typedef unsigned int UINT16_T;

    But for example i don't know the sizes of the int on different machines. Is it possible to automatically change the typedef as well. That is i want to write my program such that it should automatically change to the appropriate type for example
    for 32 bit machines

    typedef unsigned int UINT32_T;

    for 64 bit machines it should change to

    typedef unsigned int UINT64_T;

    UINT32_T changed to UINT64_T

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look into <stdint.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I have gone through the code if found something like this
    Code:
    #ifndef __int8_t_defined
    #define __int8_t_defined
    typedef signed char int8_t;
    typedef short int16_t;
    typedef int int32_t;
    #if __WORDSIZE == 64
    typedef long int64_t;
    #else
    typedef long long int64_t;
    #endif
    #endif
    but i feel somewhere i have to mention __WORDSIZE to be some value either 32 bit or 64 bit. Please correct me if i am wrong. I want to go along the following lines.
    Code:
    typedef unsigned int  tU##sizeof(unsigned int);
    I get compilation errors obviously. Please tell me if i can acheive it without mentioning the wordsize?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The point is that, assuming that you are compiling with respect to C99 or later, or your compiler has it as a standard library extension, then you should consider using typedefs from <stdint.h>:
    Quote Originally Posted by C11 Clause 7.20.1.1 Exact-width integer types
    The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two's complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

    The typedef name uintN_t designates an unsigned integer type with width N and no padding bits. Thus, uint24_t denotes such an unsigned integer type with a width of exactly 24 bits.

    These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.
    So, you don't have to "mention __WORDSIZE to be some value either 32 bit or 64 bit", and in fact __WORDSIZE is some implementation defined name that will likely cause you a compile error except for the particular implementation that you investigated. Rather, the standard library implementation, if applicable, will provide the typedefs for you. Hence, instead of defining your own UINT16_T, you would just #include <stdint.h> and use uint16_t.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Portability
    By Aparavoid in forum General Discussions
    Replies: 14
    Last Post: 08-03-2009, 09:31 AM
  2. portability
    By BarryII in forum C Programming
    Replies: 12
    Last Post: 10-02-2008, 08:48 AM
  3. Portability
    By rwmarsh in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2006, 10:36 PM
  4. Program Portability (code portability)
    By Perica in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2002, 10:03 AM
  5. what does portability mean?
    By elad in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2002, 02:46 AM

Tags for this Thread