Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between null terminated char (\0) and `^@`

My codes are like this:

#include <iostream>
using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
    cout << (int)('\0') << endl;
    cout << (char)(0) << endl;
    return 0;
}

I expected to see in terminal like this:

$ test-program
0

$ 

However, what I saw is like this:

$ test-program
0
^@
$

What makes me confusing is that I think '\0' can be converted to 0. And 0 can also be casted into \0. I expected to see a null char followed with a endl, but the result is something weird like ^@.

Does anyone have ideas about this?

like image 266
Hanfei Sun Avatar asked Dec 01 '22 04:12

Hanfei Sun


1 Answers

^@ is just how your terminal emulator renders '\0'.

like image 146
Mikhail Vladimirov Avatar answered Dec 24 '22 19:12

Mikhail Vladimirov