0

I wanted to understand what happens if we assign \00 to int in C++. I encountered this problem while executing the following code:

#include <iostream>
using namespace std;
int main()
{
int i = '\00' ; 
cout << i ; // why the output is zero?
return 0 ;
}
  • 2
    `'\00' == '\0'`; it's the NUL character. That particular initialization is no different from assigning the value `0`. – ShadowRanger Sep 04 '22 at 00:32
  • Hello ShadowRanger, Thank u for ur answer. Can you please explain the mechanism why the initialisation is setting it to 0? – Raman Kumar Sep 04 '22 at 00:39
  • 1
    It's a `char` literal that literally has the value `0` (not the ASCII character that displays as a zero, but the numeric value `0`). There's no special mechanism. You could have written `int i = (char)0;` and it would mean the exact same thing (just with a useless cast; `int i = 0;` would be the more normal way to do this). Read the duplicate, it goes over all the escapes (which apply to both string literals and char literals). – ShadowRanger Sep 04 '22 at 00:42
  • Octal zero is the same as decimal zero is the same as hexadecimal zero is the same as binary zero, etc. A number *is a number* regardless of what representation you use to write or display it - it's *the same number*. – Jesper Juhl Sep 04 '22 at 01:38

0 Answers0