Why C takes 0 prefix like var=052 as a signal that you gave octal input.

Var=052; Printf("%d\n",var); This one gives output of 42 because it takes 052 as octal input but why it takes as it but didn't take this theory with string.

1st Jan 2020, 3:52 PM
Sanjeet Kumar
Sanjeet Kumar - avatar
2 Answers
+ 4
When C was created, many different sizes of addressable memory existed. Some systems used Octal to display memory as there were a multiple of 3 bits in the addressable unit, instead of 4. There is no reason to put leading zeroes in a constant so doing it was taken as specifying a different numbering system. The first system that ran C was an octal based system so it made sense to make it the easiest base to use.
3rd Jan 2020, 3:15 AM
John Wells
John Wells - avatar
+ 3
Prefixing a integer type variable with '0' means it's Octal and '0x' means it is hexadecimal. Strings are nothing but character arrays so first you have to find the ASCII value of it then you have to find its hexadecimal value and then put them in sequence. This looks a little tedious internally and has no general use that one would use it for I guess.
1st Jan 2020, 4:23 PM
Avinesh - avatar