chapter 3 截止到整数
This commit is contained in:
parent
f0fb56c2c8
commit
0b8e953d4b
25
c03_data/altnames.c
Normal file
25
c03_data/altnames.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* 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;
|
||||||
|
}
|
13
c03_data/charcode.c
Normal file
13
c03_data/charcode.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/* charcode.c - 显示字符的代码编号 */
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("un = %ld and not %ld\n", 3000000000, 3000000000);
|
printf("un = %u and not %d\n", 3000000000, 3000000000);
|
||||||
printf("end = %d and %d\n", 200, 200);
|
printf("end = %hd and %d\n", 200, 200);
|
||||||
printf("big = %ld and not %d\n", 65537, 65537);
|
printf("big = %ld and not %hd\n", 65537, 65537);
|
||||||
printf("verybig = %ld and not %ld\n", 12345678908642, 12345678908642);
|
printf("verybig = %lld and not %d\n", 12345678908642, 12345678908642);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user