C语言学习(一)数据类型

参考书:《C Primer Plus》第六版


知识点:

  • printf("My favorite number is %d beacuse it is first.\n",num); ,其中%告诉程序把一个变量在这个位置输出,d 告诉程序将输出一个十进制整数变量。
  • 数据类型关键字:最初K&R给出的关键字:intlongshortunsignedcharfloatdouble,C90标准添加的关键字:signedvoid,C99标准添加的关键字:_Bool(布尔值)、_Complex(复数)、_Imaginary(虚数)。
  • (位)bit、(字节)byte、(字)word区分:最小的存储单元:bit,可存储0或1;最常用的计算机存储单位是byte,1byte为8bit;word是设计计算机时给定的自然存储单位,1字长从8bit到16bit到32bit再到64bit,计算机字长越大,数据转移越快,允许的内存访问也更多。
  • 显示八进制和十六进制:十进制:%d,八进制:%o,十六进制:%x,如果要显示前缀0、0x、0X应使用:%#o%#x%#X(程序清单1)
  • 所有整数类型:int、short int\short、long int\long、long long int\long long、unsigned int\unsigned、unsigned long int\unsigned long、unsigned short int\unsigned short、signed short(signed强调有符号)
  • 用后缀l或L表示long类型的数值,ll或LL表示long long类型的数值,u或U表示unsigned,如5ull、10LLU、6LLU、9Ull
  • 打印:unsigned int:%u,long:%ld,十六进制long:%lx,八进制long:%lo,short:%hd,八进制short:%ho,unsigned long:%lu,unsigned short:%hu,long long:%lld,unsigned long long:%llu
  • C语言中将单引号括起来的单个字符串称为字符常量。实际上,字符是以数值形式存储的,所以也可以使用数字代码来赋值,如’char grade=65;’,但不推荐这么做。
  • 转义序列:\a:警报,\b:退格,\f:换页,\n:换行,\r:回车,\t:水平制表符,\v:垂直制表符,\\:反斜杠,\':单引号,\":双引号,\?:问号,\0oo:八进制值(o为有效的八进制数),\xhh:十六进制值(h为有效的十六进制数,0~f中的一个数)
  • 打印时,用%d打印char类型变量的值,为一个整数,%c则打印该整数对应的字符。(程序清单2)
  • 可以在char类型前用signed或unsigned来表示有符号和无符号。
  • C语言为现有类型创建了更多的类型别名,定义在头文件stdint.h中,如int32_t表示32位的有符号整数类型,在32位int的系统中头文件会把int32_t作为int的别名,而对于int为16位、long为32位的系统会把int32_t作为long的别名。这个类型别名为精确宽度整数类型的示例。还有一种类型集合为最小宽度类型,如int_least8_t表示可容纳8位有符号整数值的类型中宽度最小的类型的别名。然后还有一种类型集合为最快最小宽度类型,如int_fast8_t为系统中对8位有符号值而言运算最快的整数类型的别名。最后,还有表示系统的最大整数类型:intmax_t为有符号的最大整数类型,以及uintmax_t为无符号的最大整数类型。另外还有相应的输入输出,如头文件inttypes.h中定义了PRId32字符串宏,代表打印32位有符号的合适转换说明。(程序清单3)
  • 假定some为float类型的变量,对于语句some=4.2*2.0;,通常4.2和2.0被存储为64位的double类型,使用双精度进行乘法运算,然后将乘积截成float类型的宽度。这样做虽然精度更高,但会拖慢程序的运行速度。在浮点数之后加f或F后缀可以覆盖默认设置。使用l或L后缀可使数字变为long double类型。C99标准新增了一种浮点型常量格式:用十六进制表示浮点型常量,在数字前加前缀0x0X,用pP分别代替eE,代表2的幂而不是10的幂。如0xa.1fp10表示(10+1/16+15/256)*1024
  • 打印浮点值时,用%f打印十进制计数法的float和double类型浮点数,用%e打印指数记法的浮点数。打印long double用%Lf%Le%La
  • C语言有3中复数类型:float_Complexdouble_Complexlong double_Complex,类似的有3种虚数类型:float_Imaginarydouble_Imaginarylong double_Imaginary
  • 要确定类型的大小,可用C语言的内置运算符sizeof,用%zd匹配sizeof的返回类型,也可用%u%lu代替。程序清单4
  • \t、\b、\r的使用示例见程序清单5

程序清单1:

#includeint main(void) {int x = 100;printf("dec=%d,octal=%o,hex=%x\n", x, x, x);printf("dec=%d,octal=%#o,hex=%#x\n", x, x, x);return 0;
}

输出:

dec=100,octal=144,hex=64
dec=100,octal=0144,hex=0x64C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 19132)已退出,代码为 0。
按任意键关闭此窗口. . .

程序清单2:

#includeint main(void) {char ch;printf("Please enter a character.\n");scanf_s("%c", &ch);printf("The code for %c is %d.\n", ch, ch);return 0;
}

输出:

Please enter a character.
c
The code for c is 99.C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 29836)已退出,代码为 0。
按任意键关闭此窗口. . .

程序清单3

#include
#include
int main(void) {char ch;int32_t me32;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);return 0;
}

输出

First,assume int32_t is int: me32=45933945
Next,let's not make any assumptions.
Instead, use a "macro" from inttypes.h: me32=45933945C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 23924)已退出,代码为 0。
按任意键关闭此窗口. . .

程序清单4

#includeint main(void) {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;
}

输出

Type int has a size of 4 bytes.
Type char has a size of 1 bytes.
Type long has a size of 4 bytes.
Type long long has a size of 8 bytes.
Type double has a size of 8 bytes.
Type long double has a size of 8 bytes.C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 4032)已退出,代码为 0。
按任意键关闭此窗口. . .

程序清单5

#includeint main(void) {float salary;printf("\aEnter your desired monthly salary: ");printf(" $________\b\b\b\b\b\b\b");scanf_s("%f", &salary);printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0);printf("\rGee!\n");return 0;
}

输出

Enter your desired monthly salary:  $_1111111Gee!    $1111111.00 a month is $13333332.00 a year.C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 20368)已退出,代码为 0。
按任意键关闭此窗口. . .

Enter your desired monthly salary:  $_1111111Gee!    $1111111.00 a month is $13333332.00 a year.C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 20368)已退出,代码为 0。
按任意键关闭此窗口. . .

练习题:

  1. 通过实验观察整数上溢、浮点数上溢和浮点数下溢。
  2. 编写一个程序,输入ASCII码的值打印输入的字符
  3. 编写一个程序,发出一声警报,然后打印文本。。。
  4. 编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式,然后打印成p计数发
  5. 一年大约有 2.156 × 1 0 7 2.156\times 10^7 2.156×107秒,编写一个程序,输入用户年龄输出对应的秒数
  6. 1个水分子质量约为 3.0 × 1 0 − 23 3.0\times10^{-23} 3.0×1023克。1夸脱水大约是950克。编写一个程序,输入水的夸脱数输出水分子的数量
  7. 1英尺相当于2.54厘米,编写一个程序,输入用户身高(英尺),然后以厘米为单位输出身高
  8. 1品脱=2杯,1杯=8盎司,1盎司=2大汤勺,1大汤勺=3茶勺。编写一个程序,输入杯数,以品脱、盎司、汤勺、茶勺为单位输出等价容量

练习1:

#include
#include
int main(void) {int ia = INT_MAX;float fvo = 3.4E38,fv1=100.0f;float fvu = 0.01234E-37,fv2=10.0f;printf("a+1=%d+1=%d\n", ia,ia + 1);//整数上溢printf("fvo * fv1=%e * %.2f= %e\n", fvo, fv1, fvo * fv1);//浮点数上溢printf("fvu / fv2=%e / %.2ff= %e\n", fvu, fv2, fvu / fv2);//浮点数下溢return 0;
}

输出

a+1=2147483647+1=-2147483648
fvo * fv1=3.400000e+38 * 100.00= inf
fvu / fv2=1.234000e-39 / 10.00f= 1.233997e-40C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 30172)已退出,代码为 0。
按任意键关闭此窗口. . .

练习2:

#include
#include
int main(void) {int ch;printf("Enter a value for ASCII\n");scanf_s("%d", &ch);printf("The char is %c", ch);return 0;
}

输出

Enter a value for ASCII
65
The char is A
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 25016)已退出,代码为 0。
按任意键关闭此窗口. . .

练习3:

#includeint main(void) {printf("\aStartled by the sudden sound,sally shouted,\n");printf("\"By the Great Pumpkin,what was that!\"");return 0;
}

输出:

Startled by the sudden sound,sally shouted,
"By the Great Pumpkin,what was that!"
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 27728)已退出,代码为 0。
按任意键关闭此窗口. . .

练习4:

#includeint main(void) {float fv;printf("Enter a floating-point value: ");scanf_s("%f", &fv);printf("fixed-point notation: %f\n", fv);printf("exponential notation: %e\n", fv);printf("p notation: %a",fv);return 0;
}

输出:

Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.0100000000000p+6
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 29272)已退出,代码为 0。
按任意键关闭此窗口. . .

练习5:

#includeint main(void) {unsigned yearToSecond = 3.156E7;unsigned year;printf("Enter your age:");scanf_s("%d", &year);printf("The second is: %d", year * yearToSecond);return 0;
}

输出

Enter your age:24
The second is: 757440000
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 26340)已退出,代码为 0。
按任意键关闭此窗口. . .

练习6:

#includeint main(void) {unsigned value;printf("Enter the size of water (quart): ");scanf_s("%d", &value);printf("the amout of hydrone is: %d", value * 950 / 3 * 1E23);return 0;
}

输出

Enter the size of water (quart): 1
the amout of hydrone is: 1857689736
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 10856)已退出,代码为 0。
按任意键关闭此窗口. . .

练习7:

#includeint main(void) {float height;printf("Enter your height: ");scanf_s("%f", &height);printf("Your height is %f cm.\n",height*2.54f);return 0;
}

输出

Enter your height: 75
Your height is 190.500000 cm.C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 17676)已退出,代码为 0。
按任意键关闭此窗口. . .

练习8:

#includeint main(void) {float num;printf("输入杯数: ");scanf_s("%f", &num);printf("%.f 杯=%.1f 品脱 =%.f 盎司=%.f 汤勺=%.f 茶勺",num,num/2.f,num*8.0f,num*16.0f,num*48.0f);return 0;
}

输出

输入杯数: 5
5=2.5 品脱 =40 盎司=80 汤勺=240 茶勺
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 30360)已退出,代码为 0。
按任意键关闭此窗口. . .


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部