13

In some platform, int32_t (from stdint.h) is long int, but in other platform, it could be int. When I want to use printf, how can I determine which format, "%ld" or "%d", should be used?

Or, perhaps, I should force converting it to long like below:

int32_t m;
m = 3;
printf ("%ld\n", (long)m);

But that solution is tedious. Any suggestions?

M.M
  • 138,810
  • 21
  • 208
  • 365
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
  • Does it have to be `printf`? Or are you simply trying to print? – Ivan Rubinson Oct 19 '16 at 03:54
  • 2
    Do you use C or C++? You can use `%s` along with [to_string](http://www.cplusplus.com/reference/string/to_string/) or `std::cout` in C++. – xuhdev Oct 19 '16 at 03:55
  • 1
    Reopened - this question is about int32_t, but the "duplicate" was about int64_t – M.M Oct 19 '16 at 03:58
  • Neither is correct. See `inttypes.h`! – too honest for this site Oct 19 '16 at 04:04
  • @xuhdev: There is absolutely no need to use C++. This is a C question, so it is pointless to recommend C++. – too honest for this site Oct 19 '16 at 04:06
  • 1
    @M.M: The approach for `int64_t` is the same as for `int32_t` and straight-forward to port. The dup was valid. It is nonsense to have a seperate question for every `stdint.h` type. – too honest for this site Oct 19 '16 at 04:07
  • 1
    @Olaf 32 and 64 are different. OP asked about both languages, which is sometimes questionable but in this case the answer is almost the same for both languages. Your opinion of whether someone else "needs to use C++" is irrelevant. – M.M Oct 19 '16 at 04:16
  • @Olaf The tag included C++ before (now removed) – xuhdev Oct 19 '16 at 04:17
  • @xuhdev: But the code did not change. – too honest for this site Oct 19 '16 at 04:18
  • 1
    Rolled back the tag vandalism. Pretty sick of people trying to force their language preferences onto OPs. – M.M Oct 19 '16 at 04:19
  • @M.M: The code is clearly C only and OP asks about the C function. Apart from the title (which shows the typical confusion), there is no indicator in the text OP wants a C++ solution. – too honest for this site Oct 19 '16 at 04:21
  • @M.M: I'm well aware that you can use **C** functions from C++. That does not make them C++ functions, though - the `extern "C" in those headers is a clear indicator. The question is obviously about C, not C++. – too honest for this site Oct 19 '16 at 04:26
  • 1
    @jww: Apparently we need one seperate question per `stdint.h` type. And the same for `scanf` ... – too honest for this site Oct 19 '16 at 04:28
  • 1
    You're wrong. OP put "C/C++" in the title and chose "C" and "C++" as tags. Therefore the question is about C and C++ regardless of any other content. `printf` is part of C++ , specified by section 27.9.2/1 of C++14, et al. (Specifying behaviour by reference to other ISO documents is common). – M.M Oct 19 '16 at 04:29
  • 3
    @jww Yeah I'm sick of it, question vandalism and overzealous duplicate-closing are among the most common complaints about SO. Along with hostility towards new users. – M.M Oct 19 '16 at 04:31
  • 1
    You could have one question that covers all of the inttypes.h stuff (perhaps with a few different examples of different sizes) , and the specific case questions could be linked to that. But I disagree with linking a int64_t question to just an int32_t answer (or vice versa) – M.M Oct 19 '16 at 04:32

1 Answers1

21

In C (since C99), the inttypes.h contains macros that expand to format specifiers for the fixed-width types. For int32_t:

printf("%" PRId32 "\n", m);

That macro is likely to expand to "d" or "ld". You can put the usual modifiers and so on, e.g.:

printf("%03" PRId32 "\n", m);

In C++ (since C++11) the same facility is available with #include <inttypes.h>, or #include <cinttypes>.

Apparently, some C++ implementations require the user to write #define __STDC_FORMAT_MACROS 1 before #include <inttypes.h>, even though the C++ Standard specifies that is not required.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Note: The macros are only defined for C++ if you `#define __STDC_FORMAT_MACROS 1` before including `inttypes.h`, at least according to the standard (some compilers provide them regardless). – ShadowRanger Oct 19 '16 at 04:09
  • @ShadowRanger They are compulsory in C according to C99, C11, and in C++ according to C++11 and C++14. Maybe you refer to footnote 182 in C99 (footnote 191 in C99+TC3). However footnotes of the C Standard are not normative for C, let alone for other languages! – M.M Oct 19 '16 at 04:14
  • Ah, didn't note that distinction. That said, I know several versions of GCC's g++ (not sure about most recent versions) definitely required the define, so portable code should define it to be sure. – ShadowRanger Oct 19 '16 at 04:15