> 文章列表 > c提高学习——字符串的注意事项

c提高学习——字符串的注意事项

c提高学习——字符串的注意事项

字符串以‘\\0’结尾
看下面几个简单的案例
c提高学习——字符串的注意事项
c提高学习——字符串的注意事项
c提高学习——字符串的注意事项
c提高学习——字符串的注意事项
如果以字符串初始化,那么编译器会默认在字符串尾部添加‘\\0’
sizeof计算数组大小,数组包括‘\\0’结束
strlen计算字符串的长度,到‘\\0’结束
c提高学习——字符串的注意事项
c提高学习——字符串的注意事项
c提高学习——字符串的注意事项
\\012是一个八进制数字 转为十进制 10 对应的ASCII码 为 换行
测试代码如下所示

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>	
#include<string.h>
#include<stdlib.h>
void test01()
{//char str1[] = { 'h','e','l','l','o','\\0'};//printf("%s\\n", str1);//char str2[100] = { 'h','e','l','l','o' };//printf("%s\\n", str2);//char str3[] = "hello";//printf("%s\\n", str3);//printf("sizeof str:%d\\n", sizeof(str3));//printf("sizeof str:%d\\n", strlen(str3));/*char str4[100] = "hello";printf("sizeof str:%d\\n", sizeof(str4));printf("sizeof str:%d\\n", strlen(str4));*///char str5[] = "hello\\0world";//printf("%s\\n", str5);//printf("sizeof str:%d\\n", sizeof(str5));//printf("sizeof str:%d\\n", strlen(str5)); char str6[] = "hello\\012world";printf("%s\\n", str6);printf("sizeof str:%d\\n", sizeof(str6));printf("sizeof str:%d\\n", strlen(str6)); 
}
int main()
{test01();system("pause");return EXIT_SUCCESS;
}