重点回顾
C语言malloc与calloc区别
区别
开门见山,malloc与calloc在内存分配时,前者分配一整块,后者分配n块,并且后者在分配时会将内存置为0,前者不会内存里是垃圾数据。
另外它们参数不同,malloc参数只有一个无符号整型的num表示分配的字节数,函数原型如下
void *malloc(size_t num);
1
calloc参数有两个,分别是无符号整型num表示分配的对象的个数,以及无符号整型size表示每个对象的大小
void *calloc(size_t num, size_t size);
1
用法
它们在用法上相似,可以起到相同的效果,并且都返回内存的首地址。
以分配容纳5个int整型的空间为例,代码如下malloc用法
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)malloc(5*sizeof(int));
for(int i=0; i<5; i++)
p[i]=i;
for(int i=0; i<5;i++)
printf("%d ",p[i]);
return 0;
}
calloc用法
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)calloc(5,sizeof(int));
for(int i=0; i<5; i++)
p[i]=i;
for(int i=0; i<5; i++)
printf("%d ",p[i]);
return 0;
}
运行结果
运行结果相同
0 1 2 3 4
————————————————
版权声明:本文为优快云博主「CairBin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/qq_42759112/article/details/124744214
字符及字符串
字符串的本质
字符串本质就是一个字符数组,只不过比字符数组多存了一个标志字符'\0'
#include <stdio.h>
int main()
{
char str_1[6]="hello";
char str_2[6]="hello\0";
char str_3[6]={'h','e','l','l','o','\0'};
printf("%s %s %s",str_1,str_2,str_3);
return 0;
}
结果
hello hello hello
--------------------------------
Process exited after 0.03257 seconds with return value 0
请按任意键继续. . .
获取字符
getchar()——带回显 换行为结束标志 putchar(ch)
getch()——不带回显 立刻返回 返回的是ASCII码,出错返回-1 需要库文件conio.h
getche()——带回显 立刻返回 返回的是ASCII码,出错返回-1 需要库文件conio.h
#include <stdio.h>
int main()
{
char ch[5];
scanf("%s",ch); //会造成缓冲区溢出
scanf("%5s",ch); //指定拿取内容的多少,避免溢出
printf("%s",ch);
return 0;
}
获取字符串gets和fgets
scanf %s时,缓存区可以无限输入,获取内容碰到空格、tab、\n、eof时,终止获取
使用gets时,无限获取缓冲区内容, 换行终止,系统会将最后的换行符拿走丢弃
fgets(数组名,数量n,文件指针)表示从文件中读出的字符不超过n-1个字符,读完后加上串结束标志'\0' stdin 键盘
打印字符串puts
无法加修饰语
puts(数组名);碰到'\0'结束 并自动加换行
字符处理函数ctype.h
#include <stdio.h>
#include <ctype.h>
void isalnum_test(void);
void isalpha_test(void);
void islower_test(void);
void isupper_test(void);
void isdigit_test(void);
void isxdigit_test(void);
/*
字符处理函数头文件ctype.h
*/
int main()
{
// isalnum_test();//是否为字母数字
// isalpha_test();//是否为字母
// islower_test(); //是否为小写字母
// isupper_test();//是否为大写字母
// isdigit_test();//是否为数字
isxdigit_test();//是否为16进制数字
return 0;
}
void isalnum_test(void)
{
char ch;
int ret;
ret=isalnum('1'); //数字返回4
ret=isalnum('a'); //小写字母返回2
ret=isalnum(' '); //其他返回0
ret=isalnum('A'); //大写字母返回1
ret=isalnum(50); //返回0
printf("ret=%d\n",ret);
}
void isalpha_test(void)
{
char ch;
int ret;
ret=isalpha('1'); //数字返回0
ret=isalpha('a'); //小写字母返回2
ret=isalpha(' '); //其他返回0
ret=isalpha('A'); //大写字母返回1
ret=isalpha(50); //返回0
printf("ret=%d\n",ret);
}
void islower_test(void)
{
char ch;
int ret;
ret=islower('1'); //数字返回0
ret=islower('a'); //小写字母返回2
ret=islower(' '); //其他返回0
ret=islower('A'); //大写字母返回0
ret=islower(50); //返回0
printf("ret=%d\n",ret);
}
void isupper_test(void)
{
char ch;
int ret;
ret=isupper('1'); //数字返回0
ret=isupper('a'); //小写字母返回0
ret=isupper(' '); //其他返回0
ret=isupper('A'); //大写字母返回1
ret=isupper(50); //返回0
printf("ret=%d\n",ret);
}
void isdigit_test(void)
{
char ch;
int ret;
ret=isdigit('1'); //数字返回1
ret=isdigit('a'); //小写字母返回0
ret=isdigit(' '); //其他返回0
ret=isdigit('A'); //大写字母返回0
ret=isdigit(50); //返回1
printf("ret=%d\n",ret);
}
void isxdigit_test(void)
{
char ch;
int ret;
ret=isxdigit('1'); //数字返回128
ret=isxdigit('a'); //小写字母返回128
ret=isxdigit(' '); //其他返回0
ret=isxdigit('A'); //大写字母返回128
ret=isxdigit(50); //返回128
printf("ret=%d\n",ret);
}