Quantcast
Viewing all articles
Browse latest Browse all 10

Answer by Bora for '\0' evaluates false, "\0" evaluates true

First of all, please note that the hexadecimal value of False is 0x00 and True is any other value than 0x00.

"\0" is a string with a character and Null Terminator '\0' at the end. So it is a character pointer, pointing to an array of 2 bytes: ['\0', '\0']. In this array, the first one is the character and the other one is the null terminator.

After compiling (without optimizing), this character pointer is temporarily assigned to an address in the memory pointing to the first byte of these two bytes. This address might be, for example, 0x18A6 in hexadecimal. So the compiler (most of them) actually writes these two values to the memory. Because a string is actually the address of the first byte of that string, our expression is interpreted as 0x18A6 != false . So, it is clear 0x18A6 != 0x00 is True.

'\0' is simply 0x00 in hexadecimal. 0x00 != 0x00 is False.

This answer is written for 8-bit data architecture with 16-bit addressing. I hope that helps.


Viewing all articles
Browse latest Browse all 10

Trending Articles