三、数据和C
4. C语言基本数据类型
4.2 其他整数类型
4.3 使用字符:char 类型
■整数常量例子
| 类型 | 十六进制 | 八进制 | 十进制 |
|---|---|---|---|
| char | \x41 | \101 | N.A. |
| int | 0x41 | 0101 | 65 |
| unsigned | 0x41u | 0101u | 65u |
| long | 0x41L | 0101L | 65L |
| unsigned long | 0x41UL | 0101UL | 65UL |
| long long | 0x41LL | 0101LL | 65LL |
| unsigned long long | 0x41ULL | 0101ULL | 65ULL |
■打印字符
/* charcode.c-显示字符的代码编号 */
#include <stdio.h>
int main(void)
{
char ch;
print("Please enter a character.\n");
scanf("%C",&ch);
print("The code for %c is %d.\n",ch,ch);
return 0;
}
//Please enter a character.
//C
//The code for C is 67.
4.4 _Bool类型
■true和false
4.5 可移植类型:stdint.h和inttypes.h
■精确宽度整数类型
■最小宽度类型
/* 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("me 32 = %" 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: me 32 = 45933945
4.6 float、double和long double
■记数法示例
| 数字 | 科学计数法 | 指数计数法 |
|---|---|---|
| 1000000000 | 1.0×10⁹ | 1.0e9 |
| 123000 | 1.23×10⁵ | 1.23e5 |
| 322.56 | 3.2256×10² | 3.2256e2 |
| 0.000056 | 5.6×10⁻⁵ | 5.6e-5 |
■声明浮点型变量
float noah,jonah;
double troble;
float plank = 6.63e-34;
long double gnp;
■浮点型常量
-1.56E+12
2.87E-3
3.14159
.2
4e16
.8E-5100.
■打印浮点值
/* showf_pt.c -- 以两种方式显示 float 类型的值 */
#include <stdio.h>
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);
// 下一行要求编译器支持C99或其中的相关特性
printf("And it's %a in hexadecimalm, power of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);
return 0;
}
//32000.000000 can be written 32000e+04
//And it's 0x1.f4p+14 in hexadecimalm, power of 2 notation
//2140000000.000000 can be written 2.140000e+09
//0.000053 can be written 53320000e-05
■浮点值的上溢和下溢
本文详细介绍了C语言中的基本数据类型,包括整数类型如char、int、unsigned long等,并展示了它们在不同进制下的表示。还探讨了_Bool类型、可移植类型如stdint.h和inttypes.h中的精确宽度整数类型。此外,文章还讲解了浮点类型如float、double和long double的使用,包括科学计数法和指数计数法的表示,以及如何打印浮点值。最后提到了浮点值可能遇到的上溢和下溢问题。

被折叠的 条评论
为什么被折叠?



