How can one print a size_t variable portably using the printf family?

前端 未结 12 963
时光说笑
时光说笑 2020-11-22 05:40

I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably?

In 32-bit ma

相关标签:
12条回答
  • 2020-11-22 06:01

    For those talking about doing this in C++ which doesn't necessarily support the C99 extensions, then I heartily recommend boost::format. This makes the size_t type size question moot:

    std::cout << boost::format("Sizeof(Var) is %d\n") % sizeof(Var);
    

    Since you don't need size specifiers in boost::format, you can just worry about how you want to display the value.

    0 讨论(0)
  • 2020-11-22 06:02

    As AraK said, the c++ streams interface will always work portably.

    std::size_t s = 1024; std::cout << s; // or any other kind of stream like stringstream!

    If you want C stdio, there is no portable answer to this for certain cases of "portable." And it gets ugly since as you've seen, picking the wrong format flags may yield a compiler warning or give incorrect output.

    C99 tried to solve this problem with inttypes.h formats like "%"PRIdMAX"\n". But just as with "%zu", not everyone supports c99 (like MSVS prior to 2013). There are "msinttypes.h" files floating around to deal with this.

    If you cast to a different type, depending on flags you may get a compiler warning for truncation or a change of sign. If you go this route pick a larger relevant fixed size type. One of unsigned long long and "%llu" or unsigned long "%lu" should work, but llu may also slow things down in a 32bit world as excessively large. (Edit - my mac issues a warning in 64 bit for %llu not matching size_t, even though %lu, %llu, and size_t are all the same size. And %lu and %llu are not the same size on my MSVS2012. So you may need to cast + use a format that matches.)

    For that matter, you can go with fixed size types, such as int64_t. But wait! Now we're back to c99/c++11, and older MSVS fails again. Plus you also have casts (e.g. map.size() is not a fixed size type)!

    You can use a 3rd party header or library such as boost. If you're not already using one, you may not want to inflate your project that way. If you're willing to add one just for this issue, why not use c++ streams, or conditional compilation?

    So you're down to c++ streams, conditional compilation, 3rd party frameworks, or something sort of portable that happens to work for you.

    0 讨论(0)
  • 2020-11-22 06:05

    Looks like it varies depending on what compiler you're using (blech):

    • gnu says %zu (or %zx, or %zd but that displays it as though it were signed, etc.)
    • Microsoft says %Iu (or %Ix, or %Id but again that's signed, etc.) — but as of cl v19 (in Visual Studio 2015), Microsoft supports %zu (see this reply to this comment)

    ...and of course, if you're using C++, you can use cout instead as suggested by AraK.

    0 讨论(0)
  • 2020-11-22 06:06
    std::size_t s = 1024;
    std::cout << s; // or any other kind of stream like stringstream!
    
    0 讨论(0)
  • 2020-11-22 06:06

    Will it warn you if you pass a 32-bit unsigned integer to a %lu format? It should be fine since the conversion is well-defined and doesn't lose any information.

    I've heard that some platforms define macros in <inttypes.h> that you can insert into the format string literal but I don't see that header on my Windows C++ compiler, which implies it may not be cross-platform.

    0 讨论(0)
  • 2020-11-22 06:08

    if you want to print the value of a size_t as a string you can do this:

    char text[] = "Lets go fishing in stead of sitting on our but !!";
    size_t line = 2337200120702199116;
    
    /* on windows I64x or I64d others %lld or %llx if it works %zd or %zx */
    printf("number: %I64d\n",*(size_t*)&text);
    printf("text: %s\n",*(char(*)[])&line);
    

    result is:

    number: 2337200120702199116

    text: Lets go fishing in stead of sitting on our but !!

    Edit: rereading the question because of the down votes i noted his problem is not %llu or %I64d but the size_t type on different machines see this question https://stackoverflow.com/a/918909/1755797
    http://www.cplusplus.com/reference/cstdio/printf/

    size_t is unsigned int on a 32bit machine and unsigned long long int on 64bit
    but %ll always expects a unsigned long long int.

    size_t varies in length on different operating systems while %llu is the same

    0 讨论(0)
提交回复
热议问题