Why C takes 0 prefix like var=052 as a signal that you gave octal input. | Sololearn: Learn to code for FREE!

0

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.

c

1/1/2020 3:52:04 PM

Sanjeet Kumar

3 Answers

New Answer

+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.

+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.