#include <stdio.h>
int main
{
char str[] = "ab\tcdef\r\n";
printf("the length of string is %d.\n", strlen(str));
printf("the sizeof of string is %d.\n", sizeof(str));
}
编译后,运行程序,输出为:
the length of string is 9.
the size of string is 10.
字符'\t','\r','\n'同样是字符,只是它们是不可见字符。所以字符串长度是6个可见字符abcdef,加上3个不可见字符,结果为9个Byte.
这个,在处理文件时,经常碰到,类似fgets之类的函数,通常会在读取的字符串后,跟一个换行&回车符.
计算字符串的大小时,别忘记了,结束符'\0'也占了一个Byte,所以,它的大小为10个Byte.