C++ 空字符('\0')和空格符(' ')
1.从字符串的长度:——>空字符的长度为0,空格符的长度为1。
2.虽然输出到屏幕是一样的,但是本质的ascii code 是不一样的,他们还是有区别的。
#include<iostream>
using namespace std;
int main(){
char a[] = " ";
char b[] = "\0";
cout << strlen(a) << endl; // 1
cout << strlen(b) << endl; // 0
char arr[] = "a b";
char brr[] = "a\0b";
cout << arr << endl; // a b //长度为 3
cout << brr << endl; // a //长度为1 ,因为遇到'\0'代表结束
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char a, b;
a = '\0';
b = ' ';
//纯输出
cout << "a: " << a << endl << "b: " << b << endl;
//ascii number
cout << "a: " << (int)a << endl; // 0
cout<< "b: " << (int)b << endl; // 32
char str1[] = { 'a', ' ', 'b','\0' };
char str2[] = { 'a', 'b', '\0'};
cout << str1 << endl; //a b
cout << str2 << endl; //ab
system("pause");
return 0;
}