stdint.h 和 inttypes.h 之间的区别

发布于 2024-12-07 08:22:30 字数 90 浏览 3 评论 0原文

stdint.h 和 inttypes.h 有什么区别?

如果没有使用它们,则无法识别 uint64_t,但使用它们中的任何一个,它都是已定义的类型。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-12-14 08:22:30

stdint.h

如果您想使用 C99 的指定宽度整数类型(即 int32_tuint16_t 等),包含此文件是“最低要求”。
如果包含此文件,您将获得这些类型的定义,以便您能够在变量和函数的声明中使用这些类型并对这些数据类型进行操作。

inttypes.h

如果包含此文件,您将获得 stdint.h 提供的所有内容(因为 inttypes.h 包含 stdint.h),但您还将获得执行的功能printfscanf(以及 fprintffscanf 等)以可移植的方式使用这些类型。例如,您将获得 PRIu64 宏,以便您可以 printf 一个 uint64_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);  
}

您想要使用 的一个原因例如,printf 与 inttypes.h 的区别是,uint64_t 在 Linux 中是 long unsigned,但在 Windows 中是 long long unsigned。因此,如果您只包含 stdint.h (而不是 inttypes.h),那么,要编写上述代码并使其在 Linux 和 Windows 之间保持交叉兼容,您将必须执行以下操作(注意丑陋的 #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
}

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
}
ˇ宁静的妩媚 2024-12-14 08:22:30

请参阅 wikipedia 文章以了解 inttypes.h。

使用 stdint.h 获取最少的定义集;如果您还需要 printf、scanf 等中的可移植支持,请使用 inttypes.h。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文