cprimer_study/c03_data/altnames.c

25 lines
1.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* altnames.c - 可移植整数类型名 */
#include <stdio.h>
#include <inttypes.h> // 支持可移植类型
int main(void)
{
int32_t me32; // me32是一个32位有符号整型变量
me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n", me32);
/*
PRId32 是一个宏,定义在<inttypes.h>这个头文件中。这个宏用于格式化输出32位有符号整型int32_t的值。
PRId32 用于确保正确的整数格式化无论在哪个平台或机器上运行都会输出32位有符号整数的正确格式。这样可以确保代码的可移植性。
如果你直接使用 %d 去打印 int32_t 类型的变量,可能在某些系统上得到的结果与预期不符,
因为 %d 可能指的是16位或64位的整数而 int32_t 始终是32位的。
使用 PRId32 可以确保总是以32位的形式打印整数。
*/
return 0;
}