daily discovery

Memory dump of new things discovered daily.

Tuesday, March 29, 2005

Equivalence of '\0' and 0 in C

Ever wondered why '\0' == 0 in C (and related languages like C++ and Objective-C) ? Well that's because when a character constant is specified in the form '\XYZ' [if X is 0 - Glen points out that X need only to be valid octal digit] then XYZ will be interpreted as a octal number, if X is x it will be interpreted as hexadecimal. Hence '\0' means octal 0, which is the same as 0, the literal decimal constant 0.

Credit goes to ni for pointing this out.

Cheers,
Steve


1 Comments:

At 11:54 PM, Glen said...

'\XYZ'

X need not be 0, it just needs to be a valid octal digit.

 

Post a Comment

<< Home