char 1个字节 范围-128~127
short 2个字节 范围-32768~32767
int 4个字节 范围-2147483648~2147483647
long 4个字节 范围-2147483648~2147483647
float 4个字节 有效位为6~7位
float 8个字节 有效位为15~16为
unsigned char 1个字节 范围 0~255
unsigned short 2个字节 范围 0~65535
unsigned int 4个字节 范围 0~4294967295
unsigned long 4个字节 范围 0~4294967295
int main(){
unsigned char a,b,c,d;
a=30;
b=a++;
c=++a;
d=10*(a++);//发生截断 d存的值为64
printf("%d %d %d\n",b,c,d);//结果为30 32 64,
return 0;
}
低字节高字节类型转换
有符号类型向无符号类型转换
低字节高字节类型转换
整形向浮点型转换
int func(){
int count=0;
unsigned int a=1;
int b=-5;
while(b++<=5){
if(a+b>5){//有符号类型先向无符号类型进行转换,再进行运算
count++;
}
}
return count;//结果为5
}
%hhd //输出char型(以数字的形式)
%hd //输出short型
%d //输出int型
%lld //输出long long型
%u //输出unsigned int型
%c //输出char型
%s //输出字符串
%f //输出float型
%lf //输出double型
%x //以16进制进行输出
%o //以8进制进行输出
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-veOSfeA8-1690986848933)(C:\Users\冉喜文\AppData\Roaming\Typora\typora-user-images\image-20230730223510263.png)]
1.申请方式不同
2.生命周期不同
3.效率不同
4.生长方向不同
5.内容不同
6.是否产生内存碎片
指针:装地址的变量
第一个
void fun(char* q) {//这种传递方式是值传递,把p的值给q,q指向了申请的空间,p还是指向空地址
q = (char*)malloc(100);
}
int main() {
char* p = NULL;
fun(p);
return 0;
}
第二个
void fun(char** q) {//这种传递方式是地址传递,*q的值就是p所指向的地址,所以这时p会指向申请的新空间
*q = (char*)malloc(100);
}
int main() {
char* p = NULL;
fun(&p);
return 0;
}
第一种
void GetMemory(char *p)
{
p=(char*)malloc(100)
}
void Test(void){
char *str=NULL;
GetMemory(str);
strcpy(str,"hello world");
printf(str);
}
//此程序会报错,跟上面第一个代码一样是值传递 p指向了申请的空间,str还是指向空地址
第二种
char* GetMemory(void){
char p[] = "hello world";
return p;
}
void Test(void){
char *str = NULL;
str = GetMemory();
printf(str);
}
//此程序会报错,因为无法返回p的地址,p已经被销毁了
第三种
void GetMemory2(char **p, int num){
*p =(char*)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str,"hello");
printf(str);
}
//输出 hello
第四种
void Test(void){
char *str = (char*)malloc(100);
strcpy(str,"hello");
free(str);
if (str != NULL){
strcpy(str, "world");
printf(str);
}
}
//输出world
观察下面代码,判断结果
int arr[]={1,2,3,4,5};
*(arr+1);//2
*(arr++);//编译不通过数组名是常量,不能进行++操作
*(&arr+1);//这个数组后面的那个数,垃圾值
int a[5][2]={(1,2),(3,4),(5,6),(7,8)};//逗号运算符,()中的两个数取右边的那个数
a[1][0]//6