常见数据类型
类型名 |
关键字 |
所占字节数 |
整型 |
int |
大多机器下4个字节,TC环境下2个字节 |
单精度实型 |
float |
4个字节 |
双精度实型 |
double |
8个字节 |
字符型 |
char |
1个字节 |
短整型 |
short |
2个字节 |
长整型 |
long |
4个字节 |
#include<stdio.h>
int main()
{
int a=1;
printf("a %d\n",sizeof(a)); //sizeof是关键字(如int,char,static),不是函数,求数据类型的长度
printf("int %d\n",sizeof(int));
printf("char %d\n",sizeof(char));
printf("double %d\n",sizeof(double));
printf("short %d\n",sizeof(short));
printf("long %d\n",sizeof(long));
printf("float %d\n",sizeof(float));
return 0;
}
a=1的存储内容。
高字节 低字节
0000 0000 |
0000 0000 |
0000 0000 |
0000 0001 |
地址:0x100 0x101 0x102 0x103
低地址 高地址
内存以字节为单位,每个字节都有一个地址,一个字节=8位(8位二进制数)
字节序
大端字节序 高字节存放在低地址
低字节存放在高地址
小端字节序 高字节存放在高地址
低字节存放在低地址
企业笔试题(待解决)
怎么判断大小端?大小端怎么转换?
char
无符号 1111 1111 2^8-1=255
0000 0000 0
有符号 最大 0111 1111 2^7-1=127
1111 1111 -127
0000 0000 0
最小 1000 0000 -128
int
无符号 最大 2^32-1
最小 0
有符号 最大 2^31-1
最小 -(2^31)
#include<stdio.h>
#include<string.h>
int main()
{
char *str="helloworld";
printf"%d\n",strlen(str)); //strlen求字符串长度遇'\0'结束
char *str="hello\0world";
printf"%d\n",strlen(str));
return 0;
}
运行
10
5
实例
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000];
int i;
for(i=0;i<1000;i++)
{
a[i]=-1-i;
printf("%d ",a[i]);
}
printf("\n");
printf("%d\n",strlen(a)); //char最小值是-128,再下一个数字是127,逐渐减到0
return 0;
}
运行
[root@localhost 24]# gcc 2-实例.c -o 2-实例
[root@localhost 24]# ./2-实例
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 -127 -128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24
255
混合运算
#include<stdio.h>
int main()
{
int i=-20;
unsigned int j=10;
printf("%d\n",i+j); //此处%d已经干预运算,使结果输出有符号整型
printf("%u\n",i+j);
if(i+j>0)
printf("i+j大于0\n");
else
printf("i+j小于0\n");
return 0;
}
运行
[root@localhost 24]# gcc 3-思考.c -o 3-思考
[root@localhost 24]# ./3-思考
-10
4294967286
i+j大于0
负数在内存里以补码的形式储存
-20 二进制 1000 0000 0000 0000 0000 0000 0001 0100
反码 1111 1111 1111 1111 1111 1111 1110 1011
补码 1111 1111 1111 1111 1111 1111 1110 1100
-20+10是在补码的十进制数+10
两种不同类型混合运算,先转换成能够表示范围较大的类型
有符号和无符号一起运算,转换成无符号
const常变量
#include<stdio.h>
#define b 20 //b为常量
int main()
{
int a=10; //a为变量
a=100;
const int c=10; //c为只读变量
//c=100不可执行,不能通过变量c修改对应内存的值,可以通过其他方式修改,比如地址
int *p=(int *)&c;
*p=100;
printf("%d\n",c); //通过地址访问c变量内的内容
return 0;
}
访问变量内容:1.通过变量名访问
2.通过地址访问
运行
[root@localhost 24]# ./4-const
100
以o开头的整型常量是8进制数
以ox开头的整型常量是16进制数
字符常量的表示方法'a','A','5'
注意'5'是5的ASCII码,‘5’和5不一样
‘\'开头的字符为转义字符
'\n'代表一个字符
'\t'空出一个tab长度
字符串常量的表示方法“a","china”
其中“a”占两个字符 a\0
例:判断位字节数有几个1
int main()
{
char ch;
int i,count=0;
scanf("%c",&ch); //从标准输入缓冲区获取数据
for(i=0;i<8;i++)
{
if(ch&1==1)
{
count=count+1;
}
ch=ch>>1;
}
printf("%d\n",count);
return 0;
}
运行
[root@localhost 24]# ./5-左移右移
a
3
a的ASCII码为97,其二进制数为0110 0001
0110 0001&0000 0001=0000 0001
将a的每一位与1相与,算出其位字节数有几个1
- 优先级
- 函数符号()数组下标【】 高优先级
- 单目运算符
- 算术运算符
- 移位运算符
- 关系运算符
- 逻辑运算符
- 三目运算符 低优先级
单目运算符
#include<stdio.h>
int main()
{
int a=4;
a+=a++;
printf("%d\n",a);
a=4;
a+=++a;
printf("%d\n",a);
a=4;
++a+=a;
printf("%d\n",a);
a=4;
++a+=a++;
printf("%d\n",a);
a=4;
++a+=++a;
printf("%d\n",a);
return 0;
}
运行
[root@localhost 24]# ./6-单目运算符
9
10
10
11
12
'\n'两个作用:1.换行 2.刷新缓冲区
fflush(stdout) //手动刷新缓冲区
格式化输出
#include<stdio.h>
int main()
{
int a=100;
float b=1.11111111;
char ch='a';
char *ptr="helloworld";
printf("%d\n",a); //整型输出
printf("%u\n",a); //无符号输出
printf("%o\n",a); //八进制输出
printf("%x\n",a); //十六进制输出
printf("%f\n",b); //浮点型输出
printf("%c\n",ch); //输出字符
printf("%s\n",ptr); //输出字符串
printf("%p\n",&a); //输出地址
printf("%10d\n",a); //左对齐,输出宽度为10
printf("%-10d\n",a); //右对齐,输出宽度为10
printf("%5.3f\n",b); //总长度5,输出3个小数。小数点也算一个长度
printf("%6.3f\n",b); //总长度6,左侧空出一个空格
printf("%3.5f\n",b); //五位小数保留,3不起作用
return 0;
}
运行
[root@localhost 24]# ./7-格式化输出
100
100
144
64
1.111111
a
helloworld
0xbfaf32e4
100
100
1.111
1.111
1.11111
if语句
#include<stdio.h>
int main()
{
int num1,num2,result;
char ch;
printf("Please input:\n");
scanf("%d%c%d",&num1,&ch,&num2);
if('+'==ch)
{
result=num1+num2;
}
else if('-'==ch)
{
result=num1-num2;
}
else if('*'==ch)
{
result=num1*num2;
}
else
{
result=num1/num2;
}
printf("result is %d\n",result);
return 0;
}
运行
[root@localhost 24]# ./8-if语句计算器
Please input:
1+1
result is 2
switch语句
#include<stdio.h>
int main()
{
int num1,num2,result;
char ch;
printf("Please input:\n");
scanf("%d%c%d",&num1,&ch,&num2);
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=num1/num2;
break;
default:
printf("error\n");
}
printf("result is %d\n",result);
return 0;
}
运行
[root@localhost 24]# ./9-switch
Please input:
1+1
result is 2
while语句
#include<stdio.h>
int main()
{
int a=10;
while(a--) //先判断再执行
{
printf("%d\n",a);
}
a=10;
do
{
printf("%d\n",a);
}
while(a--); //先执行再判断
//当循环次数不确定时,使用while语句
char ch;
int count=0;
scanf("%c",&ch);
while(1)
{
if(ch&1==1)
{
count++;
}
ch=ch>>1;
if(ch==0)
{
break; //跳出循环体
}
}
printf("%d\n",count);
return 0;
}
运行
[root@localhost 24]# ./10-while
9
8
7
6
5
4
3
2
1
0
10
9
8
7
6
5
4
3
2
1
0
a
3
for语句
#include<stdio.h>
//表达式1(执行1次)-->表达式2-->循环体-->表达式3-->表达式2,一直循环,表达式2的结果是second,没有起到限制作用
int main()
{
int i;
for (i=0,printf("first\n");i<5,printf("second\n");i++,printf("third\n"))
{
printf("%d\n",i);
}
}
运行
……
second
36684
third
second
36685
third
second
36686
third
second
36687
third
second
36688
……
无限循环
若for外层循环比内层嵌套循环多很多,外循环层到内循环层的跳跃次数过多,循环效率低。
尽量把较多的循环写在内层
break-continue语句
#include<stdio.h>
int main()
{
int i;
for(i=0;i<5;i++)
{
if(3==i)
{
//break; //跳出本层循环
continue; //结束本次循环,继续下次循环
}
printf("hello %d\n",i);
}
}
运行
[root@localhost 24]# ./12-break-continue
hello 0
hello 1
hello 2
hello 4