Thread: Cross-platform format specifier and types.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Philippines
    Posts
    20

    Post Cross-platform format specifier and types.

    I searched the internet for any %llu equivalent in Windows. I learned that I should use %I64u. I also tried searching for any ways to cross-platform a type and format specifiers. They said to use (for example) a long long integer, I should use "int64_t" instead of "long long". Also, I should use "PRId64" instead of "%lld". But one of the C programmers I emailed said that "PRId64" (and PRI?64 family) is not sufficient and may not display the correct sizes of integers. So I'm not sure. Should I keep using PRId64 (or PRIx64/PRIu64)?
    Last edited by Bryan_James; 06-07-2016 at 01:33 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In what way do you need these integer types to be cross-platform? For example, is it important that the types are guaranteed to exist (with the likewise guaranteed minimum ranges) when compiling with respect to C99? Or is it important that the types have a specific number of bits, or have at least a given number of bits?
    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
    Dec 2014
    Location
    Philippines
    Posts
    20
    I think it is important that the types and format specifiers exist that can carry the same minimum and maximum values. Does "PRIu64" and family does not display integers as long as the "unsigned long long" data type?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bryan_James
    I think it is important that the types and format specifiers exist that can carry the same minimum and maximum values.
    Unfortunately, no such guarantee exists in C. What you are guaranteed is that with a C99 (or later) conforming implementation, <stdint.h> will provide exact width integer types of 8, 16, 32, or 64 bits, if (for signed integers) these integer types exist in two's complement representation. Correspondingly, <inttypes.h> will provide macros for corresponding format specifiers.

    The problem is that in theory, some of these exact width integer types might not exist, or the signed integer representation in use might not be two's complement. In practice, implementations do not necessarily conform to the standard, e.g., the macros in <inttypes.h> might not be correct, or compilation with respect to C99 or later (we're actually at C11 now) might not be available at all.

    So, if you want to be cross-platform in the sense of "cross-platform for any platform conforming to the standard, and for which my assumptions about the types existing hold", you can go ahead and safely use int64_t along with PRId64. But, if you want cross-platform in the sense of "I have these platforms in mind, let's support them with common types and format specifiers", then you would do better to list the exact platforms, compiler versions, standard library implementations, etc, that you have in mind. Sometimes it may be that you have to conditionally define stuff yourself.

    Quote Originally Posted by Bryan_James
    Does "PRIu64" and family does not display integers as long as the "unsigned long long" data type?
    PRIu64 will be replaced by the format specifier for uint64_t. What is a uint64_t? Well, it could be an unsigned long long, but it could also be an unsigned long, unsigned int, or even an unsigned short (though unsigned char might be problematic in practice). The thing is that the minimum range of an unsigned long long is guaranteed, but its range could be greater, and while we can mathematically work out the number of bits required the support the given range (i.e., 64 bits for the minimum guaranteed range of unsigned long long), it may be possible that there are padding bits or something which uint64_t is guaranteed to not have, and of course if it has a larger range, then it is automatically unsuitable to be uint64_t since mathematically it must have a larger width.
    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. Cross-Platform GUI
    By @nthony in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2007, 02:51 PM
  2. Format Specifier
    By babu in forum C Programming
    Replies: 2
    Last Post: 06-27-2007, 11:17 PM
  3. format specifier
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 05-13-2006, 05:22 AM
  4. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 10:32 PM
  5. Cross-platform code...
    By Sul in forum C++ Programming
    Replies: 10
    Last Post: 06-18-2004, 04:44 PM

Tags for this Thread