C++空の文字('0')とスペース('')

1370 ワード

1.文字列の長さ:——>空白文字の長さは0、スペース文字の長さは1です.2.画面に出力するのは同じですが、本質的なascii codeは異なり、違いがあります.
#include
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 
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;
}

転載先:https://www.cnblogs.com/ZY-Dream/p/10028564.html