97

What is the difference between stdint.h and inttypes.h?

If none of them is used, uint64_t is not recognized but with either of them it is a defined type.

1
  • 7
    inttypes.h #includes stdint.h. Sep 29, 2011 at 12:56

2 Answers 2

180

stdint.h

Including this file is the "minimum requirement" if you want to work with the specified-width integer types of C99 (i.e. int32_t, uint16_t etc.). If you include this file, you will get the definitions of these types, so that you will be able to use these types in declarations of variables and functions and do operations with these datatypes.

inttypes.h

If you include this file, you will get everything that stdint.h provides (because inttypes.h includes stdint.h), but you will also get facilities for doing printf and scanf (and fprintf, fscanf, and so on.) with these types in a portable way. For example, you will get the PRIu64 macro so that you can printf a uint64_t like this:

#include <stdio.h>
#include <inttypes.h>
int main (int argc, char *argv[]) {

    // Only requires stdint.h to compile:
    uint64_t myvar = UINT64_C(0) - UINT64_C(1);

    // Requires inttypes.h to compile:
    printf("myvar=%" PRIu64 "\n", myvar);  
}

One reason you would want to use printf with inttypes.h is, for example, that uint64_t is long unsigned in Linux but long long unsigned in Windows. Thus, if you were to only include stdint.h (not inttypes.h), then, to write the above code and keep it cross-compatible between Linux and Windows, you would have to do the following (notice the ugly #ifdef):

#include <stdio.h>
#include <stdint.h>
int main (int argc, char *argv[]) {

    // Only requires stdint.h to compile:
    uint64_t myvar = UINT64_C(0) - UINT64_C(1);

    // Not recommended.
    // Requires different cases for different operating systems,
    //  because 'PRIu64' macro is unavailable (only available 
    //  if inttypes.h is #include:d).
    #ifdef __linux__
        printf("myvar=%lu\n", myvar);
    #elif _WIN32
        printf("myvar=%llu\n", myvar);
    #endif
}
1
  • 8
    Thanks, that was a great answer (even though I didn't ask the original question!). To add to Mikko's answer, inttypes.h copies in stdint.h (via preprocessor #include). At least on my Linux system (GCC 4.5.2 and similar).
    – pr1268
    Aug 29, 2012 at 2:02
32

See the wikipedia article for inttypes.h.

Use stdint.h for a minimal set of definitions; use inttypes.h if you also need portable support for these in printf, scanf, et al.

4
  • 27
    I came to stackoverflow to learn the difference between stdint.h and inttypes.h after reading the wiki article which tells me that (u)intN_t is available in both. So what is the difference? Which should I include?
    – dave
    Aug 15, 2012 at 6:04
  • 9
    <inttypes.h> includes <stdint.h> and adds some printf macros. See Mikko Östlund's answer below. Jun 10, 2016 at 20:52
  • I get: fatal error: inttypes.h: No such file or directory
    – m4l490n
    Apr 15, 2020 at 22:15
  • 1
    @m4l490n Are you using -std=c99?
    – Nayuki
    Sep 14, 2021 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.