20位的数组,为什么输出是27(31)呢?
程序清单://strtype4.cpp--line input
#include<iostream>
#include<string> //make string class available
#include<cstring> //C-style string library
int main()
{
using namespace std;
char charr[20];
string str;
cout<<"Length of string in charr before input: "
<<strlen(charr)<<endl;
cout<<"Length of string in str before input: "
<<str.size()<<endl;
cout<<"Enter a line of text:\n";
cin.getline(charr,20); //indicate maximum length
cout<<"You entered: "<<charr<<endl;
cout<<"Enter another line of text:\n";
getline(cin,str); //cin now an argument;no length specifier
cout<<"Length of string in charr after input: "
<<strlen(charr)<<endl;
cout<<"Length of string in str after input: "
<<str.size()<<endl;
return 0;
}
请问输出结果“Length of string in charr before input”是什么(我觉得应该是20)呢?
书上给出的结果是27
而我敲进编译器给出的结果是31
求大神指导!!!