1.由双引号引起的就是字符串
#include <stdio.h>
int main()
{
char arr1[] = "abc";//= {'a','b','c','\0'}
char arr2[] = {'a','b','c'};
printf("%d\n",sizeof(arr1));//但是他的字符串长度是3
printf("%s\n",arr1);
printf("%d\n",sizeof(arr2));//他的字符串长度是随机找到\0
printf("%s\n",arr2);
return 0;
}
输出
4
abc
3
abc
2.这是为什么呢?
char arr1[] = "abc";//= {'a','b','c','\0'}
// 原来'\0'是字符串的结束标示,'\0'的值是0
// '\0'只是一个标示不会被打印出,尽管会占用计算机内存,但是没有的话很容易出现错误
#include <stdio.h>标签:abc,C语言,char,类型,arr2,arr1,printf,字符串 From: https://blog.51cto.com/afei/6111331
#include<cstring>
int main()
{
char arr1[] = "abc";
char arr2[] = {'a','b','c'};
printf("%d\n",strlen(arr1));
printf("%s\n",arr1);
printf("%d\n",strlen(arr2));
printf("%s\n",arr2);
return 0;
}
结果3 3