From 0b8e953d4b4a2648237ef8abdcefe49941413d21 Mon Sep 17 00:00:00 2001 From: wandoubaba517 Date: Sat, 2 Dec 2023 19:27:07 +0800 Subject: [PATCH] =?UTF-8?q?chapter=203=20=E6=88=AA=E6=AD=A2=E5=88=B0?= =?UTF-8?q?=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c03_data/altnames.c | 25 +++++++++++++++++++++++++ c03_data/charcode.c | 13 +++++++++++++ c03_data/print2.c | 8 ++++---- 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 c03_data/altnames.c create mode 100644 c03_data/charcode.c diff --git a/c03_data/altnames.c b/c03_data/altnames.c new file mode 100644 index 0000000..b4a95e5 --- /dev/null +++ b/c03_data/altnames.c @@ -0,0 +1,25 @@ +/* altnames.c - 可移植整数类型名 */ +#include +#include // 支持可移植类型 + +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 是一个宏,定义在这个头文件中。这个宏用于格式化输出32位有符号整型(int32_t)的值。 + PRId32 用于确保正确的整数格式化,无论在哪个平台或机器上运行,都会输出32位有符号整数的正确格式。这样可以确保代码的可移植性。 + 如果你直接使用 %d 去打印 int32_t 类型的变量,可能在某些系统上得到的结果与预期不符, + 因为 %d 可能指的是16位或64位的整数,而 int32_t 始终是32位的。 + 使用 PRId32 可以确保总是以32位的形式打印整数。 + */ + + return 0; +} \ No newline at end of file diff --git a/c03_data/charcode.c b/c03_data/charcode.c new file mode 100644 index 0000000..1c7afe4 --- /dev/null +++ b/c03_data/charcode.c @@ -0,0 +1,13 @@ +/* charcode.c - 显示字符的代码编号 */ +#include + +int main(void) +{ + char ch; + + printf("Please enter a character.\n"); + scanf("%c", &ch); + printf("The code for %c is %d.\n", ch, ch); + + return 0; +} \ No newline at end of file diff --git a/c03_data/print2.c b/c03_data/print2.c index acdd863..3d27ee7 100644 --- a/c03_data/print2.c +++ b/c03_data/print2.c @@ -3,10 +3,10 @@ int main(void) { - printf("un = %ld and not %ld\n", 3000000000, 3000000000); - printf("end = %d and %d\n", 200, 200); - printf("big = %ld and not %d\n", 65537, 65537); - printf("verybig = %ld and not %ld\n", 12345678908642, 12345678908642); + printf("un = %u and not %d\n", 3000000000, 3000000000); + printf("end = %hd and %d\n", 200, 200); + printf("big = %ld and not %hd\n", 65537, 65537); + printf("verybig = %lld and not %d\n", 12345678908642, 12345678908642); return 0; } \ No newline at end of file