How do i set zero as a Integer?

G

gk245

I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.

for example:

int 08453

should print 08453 if i tried to use printf() on it, and not some octal
number.

Thanks.
 
E

Emmanuel Delahaye

gk245 a écrit :
I mean, anything that follows a 0 is automatically turned into a octal
number.

Badly phrased.

<<anything that follows a 0 is automatically interpreted like the
textual representation of an octal number.>>
I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.

for example:

int 08453

This is not C. What do you meant ?

int x = 08453;

? 'Invalid octal representation'

int x = 8453;

Is a valid decimal representation.
should print 08453 if i tried to use printf() on it, and not some octal
number.

How is it important to you to have this leading 0 in a source code ?
 
V

Vladimir S. Oka

gk245 said:
I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.

I'm not sure I understand what you think is the problem, as integer
variables /always/ store values as integers, but let's try this:
for example:

int 08453

This has no meaning in C. Did you mean:

int i = 08453;
should print 08453 if i tried to use printf() on it, and not some
octal number.

In which case you should have written either of these:

int i = 8453;
int i = 020405;
int i = 0x2105;

An integer variable holds an integer /value/. This is different from
integer /constants/ (literals) which can be represented in different
bases. All the following are equivalent:

int i = 07;
int i = 0x7;
int i = 7;

They all assign value 7 (seven, as in "seven apples") to `i`. This
means, when you print `i` using printf(), you get its /value/ out in
whatever format you specify:

printf("%d\n",i); /* decimal */
printf("%x\n",i); /* hex; it's /not/ preceeded by 0x, though */
...

So, you can have it any way you want it, really.
 
V

Vladimir S. Oka

Vladimir said:
I'm not sure I understand what you think is the problem, as integer
variables /always/ store values as integers, but let's try this:


This has no meaning in C. Did you mean:

int i = 08453;

D'oh! You caught me out here! The above is not a valid octal number.
Octal digits are [0..7] only. Everything else still holds, though...
 
G

gk245

gk245 a écrit :

Badly phrased.

<<anything that follows a 0 is automatically interpreted like the
textual representation of an octal number.>>


This is not C. What do you meant ?

int x = 08453;

? 'Invalid octal representation'

int x = 8453;

Is a valid decimal representation.


How is it important to you to have this leading 0 in a source code ?

Sorry, i meant int x = 08453;

Well, say you want the user to enter a number that starts with 0, then
have that number displayed properly using printf(). Is that not something
a variable set to integer would do? Or should i be using char instead?
Ofcourse, you couldn't do any calculations with char in the future if you
wanted to, so i was thinking it was better to leave it as a integer.

Is there a way, then, to truncate the leading zero when a user enters a
zero?

for example, like this:

printf("Enter Integer: \n");
scanf("%i\n", &var1);

Now, what if the user entered a few zeros before entering a Integer? I
can't use if-else, since its not allowed yet for me to use.
 
E

Emmanuel Delahaye

gk245 a écrit :
Sorry, i meant int x = 08453;

Well, say you want the user to enter a number that starts with 0, then
have that number displayed properly using printf().

This is completely different. Use fgets() and strtol() or strtoul(). If
you choose the base (say 10), the leading zeros will not be interpreted
a an octal representation heading, and the conversion will succed.
 
E

Eric Sosman

gk245 wrote On 02/10/06 13:04,:
I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.

for example:

int 08453

should print 08453 if i tried to use printf() on it, and not some octal
number.

You're mixing up two different things.

First, an integer variable just holds integer values.
As it happens, C requires integer variables to use binary
representation, but that's very nearly irrelevant: if it
were not for a few operators like ^ and >>, whose behavior
is difficult to describe in base three, say, there would
be no need for C to have such a requirement. If you're
not using these "bit-defined" operations, you can (and,
I'd say, should) simply forget about the number base in
most instances. So: an integer variable holds integer
values, not decimal values or octal values.

Second, C programs use various source-code notations
that allow the programmer to specify values. There are
different notations for different types of value (for
example, "abc" is the notation for a string value, 1.5
is the notation for a double value). Some types have
more than one notation: 1.5 and 0.15e1 and 15e-1 are
different ways of writing one-and-a-half as a double
constant. For integers there are three ways[*] to denote
an integer constant: in decimal, octal, or hexadecimal
base. (Actually, there are a few other ways. Without
any intent to ridicule you, I suspect you may not be ready
to learn about them yet and I'd prefer not to add to your
confusion just now.)

The important thing to note is that the form of the
notation is part of the C language. When you start a
number with a zero digit, you have announced to the
compiler that you intend to use octal notation to express
the desired value. Start it with zero and X and you
declare that you're using hexadecimal. Start it with a
non-zero digit and you say you're using decimal. That's
it, the unalterable It: it's the convention of C source
code notations. The conventions are in a sense arbitrary
(some other languages, for example, use notations like
'X'C and 'O'14 -- I have even seen twelve written as
"(2)300", the double quotes being part of the notation).
However, once the conventions are chosen they cannot be
changed. They exist as a means of communicating your
intent to the compiler; if you say one thing but mean
another, the compiler will fail to understand you properly.

Finally, observe that these conventions apply only
in C source code (and to a few library functions). There
is no rule saying that the numbers you print must follow
the same conventions that C source does. For example,
try printf("%05d\n", 8453) and see if you like what you get.
 
G

gk245

Vladimir said:
I'm not sure I understand what you think is the problem, as integer
variables /always/ store values as integers, but let's try this:


This has no meaning in C. Did you mean:

int i = 08453;

D'oh! You caught me out here! The above is not a valid octal number.
Octal digits are [0..7] only. Everything else still holds, though...
In which case you should have written either of these:

int i = 8453;
int i = 020405;
int i = 0x2105;

An integer variable holds an integer /value/. This is different from
integer /constants/ (literals) which can be represented in different
bases. All the following are equivalent:

int i = 07;
int i = 0x7;
int i = 7;

They all assign value 7 (seven, as in "seven apples") to `i`. This
means, when you print `i` using printf(), you get its /value/ out in
whatever format you specify:

printf("%d\n",i); /* decimal */
printf("%x\n",i); /* hex; it's /not/ preceeded by 0x, though */
...

So, you can have it any way you want it, really.

I just found something out...you used %d, and i was using %i. For some
reason or other, %d displays the integer after truncating the leading
zeros. %i doesn't...if you feed it anything beginning with a 0, it turns
the output into. So, i had this:

printf("%i\n",i);

Changing it to:

printf("%d\n",i);

Works wonderfully. Thanks. Not sure why %i doesn't work.
 
N

Nelu

gk245 said:
Sorry, i meant int x = 08453;

Well, say you want the user to enter a number that starts with 0, then
have that number displayed properly using printf(). Is that not
something a variable set to integer would do? Or should i be using char
instead? Ofcourse, you couldn't do any calculations with char in the
future if you wanted to, so i was thinking it was better to leave it as
a integer.

Is there a way, then, to truncate the leading zero when a user enters a
zero?

for example, like this:

printf("Enter Integer: \n");
scanf("%i\n", &var1);

Now, what if the user entered a few zeros before entering a Integer? I
can't use if-else, since its not allowed yet for me to use.
%i reads an int * in base 0, meaning that (strtol) 0xABC reads a hexa
number and 0ABC reads an octal number and anything else reads a base
10 number. The leading 0 has no meaning for an integer number. If you
want to use int as a code for something that should allow 0 as the
leading digit then you can't do that. You should store the digits as char *.
 
V

Vladimir S. Oka

gk245 said:
Sorry, i meant int x = 08453;

Why are you interested in octal numbers, and how do you plan to deal
with them, if you don't know they can't have `8` in them?
Well, say you want the user to enter a number that starts with 0, then
have that number displayed properly using printf(). Is that not
something a variable set to integer would do? Or should i be using
char instead? Ofcourse, you couldn't do any calculations with char in
the future if you wanted to, so i was thinking it was better to leave
it as a integer.

If you know what base your user will use, you can use appropriate format
in scanf(): %d for decimal, %o for octal, and %x for hex.
Is there a way, then, to truncate the leading zero when a user enters
a zero?

You cold get user input as a string (NB, do /not/ use scanf() for this,
as it can overrun space you allocate for the string -- rather, use
fgets()/sscanf(); for the same reason do /not/ use gets()). Once you
have that, you can parse that string to your heart's content.
for example, like this:

printf("Enter Integer: \n");
scanf("%i\n", &var1);

("%i" is rarely used these days)
Now, what if the user entered a few zeros before entering a Integer?
I can't use if-else, since its not allowed yet for me to use.

As you've written it, the number will still be interpreted as decimal.
It's the format (conversion) specifier in scanf() that matters.
 
V

Vladimir S. Oka

gk245 said:
I just found something out...you used %d, and i was using %i. For
some reason or other, %d displays the integer after truncating the
leading
zeros. %i doesn't...if you feed it anything beginning with a 0, it
turns
the output into. So, i had this:

printf("%i\n",i);

Changing it to:

printf("%d\n",i);

Works wonderfully. Thanks. Not sure why %i doesn't work.

I don't understand: %i should be the same as %d for printf(). It /is/
different for scanf() (at least on my gcc/libc; couldn't find it in C99
-- any takers?), and it'll give you the behaviour you say don't like
(i.e. it'll interpret 0nnn as octal, and 0xnnn as hex). One lives and
learns...
 
T

tmp123

Emmanuel said:
gk245 a écrit :

This is completely different. Use fgets() and strtol() or strtoul(). If
you choose the base (say 10), the leading zeros will not be interpreted
a an octal representation heading, and the conversion will succed.

Or try scanf with "%d" instead of "%i".
 
F

Flash Gordon

Vladimir said:
Vladimir said:
gk245 wrote:
This has no meaning in C. Did you mean:

int i = 08453;

D'oh! You caught me out here! The above is not a valid octal number.
Octal digits are [0..7] only. Everything else still holds, though...

<snip>

Actually as a historical odity, if you use K&R C then 8 *is* a valid
octal digit.

To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in
my copy), "Everyone's favourite trivial change: 8 and 9 are not octal
digits."
 
E

Emmanuel Delahaye

gk245 a écrit :
Not sure why %i doesn't work.

It works, but differently. actually scanf() with "%i", like strtol()
with base 0, interprets the string like in a C-source (handy if you are
writing a c-compiler!).

if 0x
hexa
else if 0
octal
else
decimal
endif

Why don't you read a C-book ? It's well explained...
 
G

gk245

Yeah, i was using scanf("%i" &var1); to get the input from the user, then i
passed that to printf("%i", var1);. This causes the wrong behaviour if the
input from the user to scanf() begins with 0's. Not sure what printf() was
putting out with %i, but i had assumed it was octals. Guess %i might be
obsolete..or the compiler just doesn't like it (MinGW).
 
E

Emmanuel Delahaye

gk245 a écrit :
Not sure what printf() was
putting out with %i, but i had assumed it was octals. Guess %i might be
obsolete..or the compiler just doesn't like it (MinGW).

%i and %d with printf() and an int have exactly the same behaviour.
 
V

Vladimir S. Oka

gk245 said:
Yeah, i was using scanf("%i" &var1); to get the input from the user,
then i passed that to printf("%i", var1);. This causes the wrong
behaviour if the input from the user to scanf() begins with 0's. Not
sure what printf() was putting out with %i, but i had assumed it was
octals. Guess %i might be obsolete..or the compiler just doesn't like
it (MinGW).

Please don't top-post...

No, it's not wrong. It's just as it's supposed to be.

Using %d or %i with printf() should produce the same result, AFAIK (for
octal use %o, for hex %h). It's the scanf() that behaves differently
for the two (do heed all the warnings you got for not using scanf(),
though).
 
V

Vladimir S. Oka

Flash said:
Vladimir said:
Vladimir said:
gk245 wrote:
for example:

int 08453
This has no meaning in C. Did you mean:

int i = 08453;

D'oh! You caught me out here! The above is not a valid octal number.
Octal digits are [0..7] only. Everything else still holds, though...

<snip>

Actually as a historical odity, if you use K&R C then 8 *is* a valid
octal digit.

To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in
my copy), "Everyone's favourite trivial change: 8 and 9 are not octal
digits."

That's interesting! Unfortunately, I don't have a copy of K&R1, and
(probably for that reason) have never bother reading Appendix C in
K&R2.

And what values did 8 and 9 yield according to K&R1?
 
G

gk245

gk245 wrote On 02/10/06 13:04,:
I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.

for example:

int 08453

should print 08453 if i tried to use printf() on it, and not some octal
number.

You're mixing up two different things.

First, an integer variable just holds integer values.
As it happens, C requires integer variables to use binary
representation, but that's very nearly irrelevant: if it
were not for a few operators like ^ and >>, whose behavior
is difficult to describe in base three, say, there would
be no need for C to have such a requirement. If you're
not using these "bit-defined" operations, you can (and,
I'd say, should) simply forget about the number base in
most instances. So: an integer variable holds integer
values, not decimal values or octal values.

Second, C programs use various source-code notations
that allow the programmer to specify values. There are
different notations for different types of value (for
example, "abc" is the notation for a string value, 1.5
is the notation for a double value). Some types have
more than one notation: 1.5 and 0.15e1 and 15e-1 are
different ways of writing one-and-a-half as a double
constant. For integers there are three ways[*] to denote
an integer constant: in decimal, octal, or hexadecimal
base. (Actually, there are a few other ways. Without
any intent to ridicule you, I suspect you may not be ready
to learn about them yet and I'd prefer not to add to your
confusion just now.)

The important thing to note is that the form of the
notation is part of the C language. When you start a
number with a zero digit, you have announced to the
compiler that you intend to use octal notation to express
the desired value. Start it with zero and X and you
declare that you're using hexadecimal. Start it with a
non-zero digit and you say you're using decimal. That's
it, the unalterable It: it's the convention of C source
code notations. The conventions are in a sense arbitrary
(some other languages, for example, use notations like
'X'C and 'O'14 -- I have even seen twelve written as
"(2)300", the double quotes being part of the notation).
However, once the conventions are chosen they cannot be
changed. They exist as a means of communicating your
intent to the compiler; if you say one thing but mean
another, the compiler will fail to understand you properly.

Finally, observe that these conventions apply only
in C source code (and to a few library functions). There
is no rule saying that the numbers you print must follow
the same conventions that C source does. For example,
try printf("%05d\n", 8453) and see if you like what you get.


Wow, thx for the big explanation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top