C stdint.h和inttypes.h之间的差异
stdint.h和inttypes.h之间有什么区别 如果未使用其中任何一种类型,则无法识别uint64,但使用其中任何一种类型都是已定义的类型。 使用stdint.h获得一组最小的定义;如果您还需要在printf、scanf等中为这些文件提供便携支持,请使用inttypes.h。stdint.h 如果要使用指定宽度的整数类型C99(即C stdint.h和inttypes.h之间的差异,c,uint64,stdint,C,Uint64,Stdint,stdint.h和inttypes.h之间有什么区别 如果未使用其中任何一种类型,则无法识别uint64,但使用其中任何一种类型都是已定义的类型。 使用stdint.h获得一组最小的定义;如果您还需要在printf、scanf等中为这些文件提供便携支持,请使用inttypes.h。stdint.h 如果要使用指定宽度的整数类型C99(即int32\u t,uint16\u t等),则包含此文件是“最低要求”。 如果包含此文件,则将获得这些类型的定义,以便能够在变量和函数声明中使用这些类型,并对这
int32\u t
,uint16\u t
等),则包含此文件是“最低要求”。
如果包含此文件,则将获得这些类型的定义,以便能够在变量和函数声明中使用这些类型,并对这些数据类型执行操作
inttypes.h
如果包含此文件,将获得stdint.h提供的所有内容(因为inttypes.h包括stdint.h),但是还将获得以可移植方式使用这些类型执行printf
和scanf
(和fprintf
,fscanf
,等等)的工具。例如,您将获得PRIu64
宏,这样您就可以printf
auint64\u t
如下所示:
#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);
}
inttypes.h
#include
s stdint.h.I在阅读了wiki文章后来到stackoverflow学习stdint.h和inttypes.h之间的区别,这篇文章告诉我,(u)intN\t在这两种版本中都可用。那么有什么区别呢?我应该包括哪些?包括并添加一些printf宏。请参阅下面MikkoÖstlund的答案。我得到:致命错误:inttypes.h:没有这样的文件或目录
谢谢,这是一个很好的答案(尽管我没有问最初的问题!)。为了补充Mikko的答案,inttypes.h复制到stdint.h中(通过预处理器#include
)。至少在我的Linux系统上(GCC4.5.2和类似版本)。
#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
}