-2

Hello it may be some silly question, but this is bugging me since few days.

I have bellow line of code:

static const char x1 = static_cast<int>(-15);
cout<<x1;

static const char x2= 16;
cout<< "hello "<< x2<<'\n';

The output is None I mean nothing is getting printed in console. But when I did the comparison like if(kill == -15) its evaluated as True. So may I know why I can not see any output in console when I am printing.

Prashant Pathak
  • 173
  • 1
  • 2
  • 12

1 Answers1

0

If you are printing a char, it is taken as an ASCII value. I.e., std::cout << char{66}; would print B.

If you want to print your char as a number, simply prepend +.

mrks
  • 8,033
  • 1
  • 33
  • 62
  • 3
    The C++ standard does not guarantee that it will use ASCII. It could be ASCII (and very often is) but it is not guaranteed. – Borgleader Aug 15 '18 at 17:57
  • @mrks What is the magic behind + as a prefix, please explain. – Prashant Pathak Aug 15 '18 at 19:04
  • 1
    @PrashantPathak I'd go doe something more explicit and obvious like a cast, but `char` is an integer type with some special handling when displayed . When you perform arithmetic on a `char` it is promoted to an `int` and is printed out as an `int`. Some examples: https://ideone.com/RBInD8 – user4581301 Aug 15 '18 at 19:34