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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM