sizeof考虑“\0”,strlen不考虑“\0”
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int main(){ enum m{a,b,c,d,e,f,g,h,i,j,k,l}; cout << sizeof(m) <<endl;//4 枚举类型类似于指针 cout << sizeof("\0\0\0") <<endl;//4 3个\0 + 末尾的\0 = 4 cout << sizeof("\01234") <<endl;//4 \012被认为是8进制所以是一个字符 + (3) + (4) + (\0) = 4 cout << strlen("\0") << endl;//0 char str[]="ab\n\012\\\""; // a,b,\n,\012,\\,\",\0 = 7 cout << sizeof(str) << endl;//7 cout << sizeof("\x12345656777777");//2这里的\x后面的4个数被认为是16进制,后面的被认为是16进制越界了。不做处理 return 0; }
记录每天的收获,from now on!