suresh said:
What do you mean by 'storing it as an octal number' ?
The representation of a number dose not matter for C. Typically it is
stored as binary number.
Indeed, it is required to be stored as a binary number, or at least
that the implementation behave "as if" that were the case.
I have seen lot of beginners having this confusion with octal and
hexadecimal representations. It is better to be cleared off this
confusion in the beginning it self.
Absolutely. I recently had a similar problem where someone was trying
to strip leading zeroes from decimal numbers before printing them back
out. So their first-draft test had something like:
long var = 008;
Which of course caused compile-time errors.
For the original poster: it does sound as if you're confusing storage
with representation. The only way to store something as an "octal
number" is to store it in a string representation of octal, which is
hardly efficient.
What you /really/ want to do is read it into a /binary/ format (some
sort of integer type), using scanf() or strtol() or similar (I and
probably many others here would /strongly/ recommend against any of
the *scanf() funcitons--few really know how to use it properly, and
those who do know how typically don't bother, since strtol() and its
cousins are so much more flexible). Once you've done that, you have
everything you need to know about the numnber.
When it comes time to print it out again, you simply use the
/appropriate/ conversion specification in printf() or whatever:
namely, "%o" (possibly modified according to your specific needs). Be
aware that the "o" specifier wants an unsigned int. And, if you want
it to be easily recognizable as an octal number, you probably want to
use the "#" flag (as in "%#o") as well.
In summary, it doesn't matter /what/ kind of number it is: if you read
it in correctly, it is that number, regardless of what kind of
representation you plan to use to print it out. The representation
used to print it back out merely depends on what conversion
specification you're using with *printf().
Hope that helps...
-Micah