difference between stdint.h and inttypes.h

牧云@^-^@ 提交于 2019-11-28 15:51:58

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.

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 "PRIu16" macro so that you can printf an uint16_t integer like this:

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

    // Only requires stdint.h to compile:
    uint16_t myvar = 65535;

    // Requires inttypes.h to compile:
    printf("myvar=%" PRIu16 "\n", myvar);  
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!