Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space vs null character

Tags:

c++

null

space

In C++, when we need to print a single space, we may do the following:

cout << ' ';

Or we can even use a converted ASCII code for space:

cout << static_cast<char>(32); //ASCII code 32 maps to a single space

I realized that, printing a null character will also cause a single space to be printed.

cout << static_cast<char>(0); //ASCII code 0 maps to a null character

So my question is: Is it universal to all C++ compilers that when I print static_cast<char>(0), it will always appear as a single space in the display?

If it is universal, does it applies to text files when I use file output stream?

like image 769
user3437460 Avatar asked Dec 09 '22 07:12

user3437460


1 Answers

No, it will be a zero(0) character in every compiler. Seems that the font you use renders zero characters as a space. For example, in the old times, DOS had a different image (an almost filled rectangle) for zero characters.

Anyway, you really should not output zero characters instead of spaces!

As for the text file part: open the outputted file using a hex editor to see the actual bits written. You will see the difference there!

like image 105
Csq Avatar answered Dec 22 '22 16:12

Csq