//一方面字符数组可以看成普通数组
char arr[]={'a','b','c','d','f'};
//另一方面字符串都被存储在char类型数组中。字符数组必须以'\0'字符结束,表示一个字符串的结束
//方式1:标准写法
char str1[6]={'h','e','l','l','o','\0'};
//方式2: 简化写法
//自动在末尾添加‘\0’
char str2[12]={"hello world"};
//或者
char str3[12]="hello world";
//进一步简化
char str5[]="hello world";
//字符串对应字符数组的长度
printf("%d\n",sizeof(str3)/sizeof(char));//12
printf("%d\n",sizeof(str5)/sizeof(char));//12
//字符串的长度
printf("%d\n",strlen(str5));//11,值为字符串的实际长度,不包含‘\0’
char nation[10]="China";
printf("%d\n",sizeof(nation)/sizeof(char));//10字符串对应的数组
printf("%d\n",strlen(nation));//5字符串长度
puts(str5);
字符数组字符串
最新推荐文章于 2024-05-31 10:46:10 发布