I wrote a piece of code and tested with gcc compiler
#include <iostream>
int main()
{
char arr[ 1000 ];
for( int index( 0 ); index < 1000; ++index )
{
std::cout << arr[ index ] << std::endl;
}
return 0;
}
I was hoping it to print the garbage values but to my surprise, it did not print anything. When I simply changed the datatype of arr from char to int, it displayed the garbage values as expected. Could somebody please explain this to me?
The overloads for
<<for character types do not treat them asintegral types, but as characters. If the garbage value
corresponds to a printable character (e.g. 97, which corresponds
to
'a'), you will see it. If it doesn’t (e.g. 0), you won’t.And if the garbage values correspond to some escape sequence
which causes your terminal to use a black foreground on a black background, you won’t see anything else, period.
If you want to see the actual numerical values of a
char(orany character type), just convert the variable to
intbeforeoutputting it: