From 224dda5a69641cd56e8f9644127dd2994cbef923 Mon Sep 17 00:00:00 2001 From: wandoubaba517 Date: Sun, 3 Dec 2023 21:25:17 +0800 Subject: [PATCH] chapter 3 done --- c03_data/escape.c | 15 +++++++++++++++ c03_data/floaterr.c | 14 ++++++++++++++ c03_data/showf_pt.c | 16 ++++++++++++++++ c03_data/typesize.c | 15 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 c03_data/escape.c create mode 100644 c03_data/floaterr.c create mode 100644 c03_data/showf_pt.c create mode 100644 c03_data/typesize.c diff --git a/c03_data/escape.c b/c03_data/escape.c new file mode 100644 index 0000000..70e1aac --- /dev/null +++ b/c03_data/escape.c @@ -0,0 +1,15 @@ +/* escape.c - 使用转移序列 */ +#include + +int main(void) +{ + float salary; + + printf("\aEnter your desired monthly salary:"); + printf("$______\b\b\b\b\b\b"); + scanf("%f", &salary); + printf("\n\t$%.2f a month is $%.2f a year?", salary, salary * 12.0); + printf("\r Gee!\n"); + + return 0; +} \ No newline at end of file diff --git a/c03_data/floaterr.c b/c03_data/floaterr.c new file mode 100644 index 0000000..019e598 --- /dev/null +++ b/c03_data/floaterr.c @@ -0,0 +1,14 @@ +/* floaterr.c - 演示舍入错误 */ +#include + +int main(void) +{ + float a, b; + + b = 2.0e20 + 1.0; + a = b - 2.0e20; + + printf("%f \n", a); + + return 0; +} \ No newline at end of file diff --git a/c03_data/showf_pt.c b/c03_data/showf_pt.c new file mode 100644 index 0000000..0750c92 --- /dev/null +++ b/c03_data/showf_pt.c @@ -0,0 +1,16 @@ +/* showf_pt.c - 以两种方式显示float类型的值 */ +#include + +int main(void) +{ + float aboat = 32000.0; + double abet = 2.14e9; + long double dip = 5.32e-5; + + printf("%f can be written %e\n", aboat, aboat); + printf("And it's %a in hexadecimal, powers of a notation\n", aboat); + printf("%f can be written %e\n", abet, abet); + printf("%Lf can be written %Le\n", dip, dip); + + return 0; +} \ No newline at end of file diff --git a/c03_data/typesize.c b/c03_data/typesize.c new file mode 100644 index 0000000..ae777ec --- /dev/null +++ b/c03_data/typesize.c @@ -0,0 +1,15 @@ +/* typesize.c - 打印类型大小 */ +#include + +int main(void) +{ + // %zd 用于打印size_t类型的值。sizeof(int) 返回一个 size_t 类型的值,表示 int 类型变量的大小(以字节为单位)。 + printf("Type int has a size of %zd bytes.\n", sizeof(int)); + printf("Type char has a size of %zd bytes.\n", sizeof(char)); + printf("Type long has a size of %zd bytes.\n", sizeof(long)); + printf("Type long long has a size of %zd bytes.\n", sizeof(long long)); + printf("Type double has a size of %zd bytes.\n", sizeof(double)); + printf("Type long double has a size of %zd bytes.\n", sizeof(long double)); + + return 0; +} \ No newline at end of file