7.1 整型
整型分为有符号和无符号,其中对于有符号来说,最左边的位标识正负,符号位为 0 表示正数或者 0,符号位为 1 表示负数。
默认情况下都是有符号的变量,需要使用无符号的变量则要声明 unsigned
#include <stdio.h>
int main()
{
int i=0;
int j=1;
printf("%d\n",i-j);
}
// 输出
-1
更多的整型
这些整型前面的修饰符可以互换顺序,也可以缩写
#include <stdio.h>
int main()
{
short int i = 65535;
printf("%d",i);
}
// 输出,因为已经超过了 short int 可以容纳的范围了
-1
7.1.1 整型常量
八进制必须以 0 开头017,0377,077777
十六进制必须以 0x 开头,且大小写无所谓0xfF
计算时,甚至可以混用这些书写形式
7.1.2 读写整型
#include <stdio.h>
int main()
{
int i = 15;
printf("有符号:%d\n",i);
printf("无符号:%u\n",i);
printf("八进制:%o\n",i);
printf("十六进制:%x\n",i);
}
// 输出
有符号:15
无符号:15
八进制:17
十六进制:f
7.2 浮点型
7.3 字符型
利用库函数转换大小写
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch = 'c';
printf("%c",toupper(ch));
return 0;
}
// 输出
C
7.3.1 读写字符
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
char ch2;
scanf("%c",&ch);
ch2 = getchar();
printf("\n%c,%c",ch,ch2);
}
// 输入
ab
// 输出
a,b
7.4 sizeof
用于确定类型的占用空间大小
int main()
{
char ch;
int i;
float f;
printf("%d\n",sizeof(ch));
printf("%d\n",sizeof(i));
printf("%d\n",sizeof(f));
}
// 输出
1
4
4
7.5 类型转换
类型转换分为隐式转换和显式转换
隐式转换是自动的,当低精度->高精度时,是可以自动进行的
#include <stdio.h>
int main()
{
int i = 10;
float j;
j=i;
printf("%f",j);
}
// 输出
10.000000
显式转换(强制类型转换)
即强制把某种类型转换成另一种类型
#include <stdio.h>
int main()
{
int i;
float j = 10.0;
i=(int)j;
printf("%d",i);
}
// 输出
10