Formatted IO; %d or %i?

A

August Karlstrom

Is there any difference between `%d' and `%i' in formatted IO? Which one
is preferred?

Regards,
August
 
C

Chris Torek

Is there any difference between `%d' and `%i' in formatted IO? Which one
is preferred?

For the printf family of functions: there is no difference whatsoever.
(I use %d mostly out of habit, because K&R C did not have %i.)

For the scanf family of functions: the difference between %d and
%i is essentially the same as that between using strtol() with a
base of 10 (%d) or 0 (%i). That is, with %i, the number 0xa0 is
valid and has the value 160 decimal, and 031 is octal and has the
value 25 decimal. (This is, of course, why so many programmers
confuse Christmas and Halloween: DEC 25 equals OCT 31.)
 
C

Chris Torek

How does one count in base 0?

Heh.

The quote above trims too much: I remember mentioning the strtol()
function specifically, so that the word "base" refers to its third
argument:

long strtol(const char *nptr, char **endptr, int base);

When the base argument is 0, strtol() uses the same "leading 0x means
hex, leading 0 means octal, otherwise the number is decimal" rule that
the C language itself uses.
 
M

Michael Mair

Mark said:
How does one count in base 0?

Unfortunately, you snipped the relevant part; I'll quote it for
reference:
,- Chris Torek: ----
| For the scanf family of functions: the difference between %d
| and %i is essentially the same as that between using strtol()
| with a base of 10 (%d) or 0 (%i).
| [...]
`----
base 10/base 0 just refers to the base parameter of strtol():
#include <stdlib.h>
long strtol(const char *str, char **ptr, int base);
i.e.
sscanf(str, "%i", ....) <-> strtol(str, NULL, 0)
sscanf(str, "%d", ....) <-> strtol(str, NULL, 10)

Following the above, Chris also explained the difference; in
short, base=10 expects a base 10 number and will scan 0x57 as
0 and 057 as 57, whereas base=0 means "try to intelligently guess
whether the number is base 8, 10, or 16". So, 0x57 will be 57
hexadecimal and 057 will be 57 octal.


BTW: You cannot work sensibly with base 1 and not at all with base 0.


Cheers
Michael
 
L

lawrence.jones

Michael Mair said:
BTW: You cannot work sensibly with base 1

You can if all you need to do is count -- have you never used hash/tally
marks?

-Larry Jones

Oh, what the heck. I'll do it. -- Calvin
 
M

Michael Mair

You can if all you need to do is count -- have you never used hash/tally
marks?

Nope, so I do not know about that.

In base 1, every digit can take values from 0 to base-1=0 and digit
n (counting from 0) represents the multiples of 1^n=1.
So, we can only represent the value 0 which does not make much sense
in terms of counting. If you want to count occurrences of 0, you are
not performing base 1 arithmetics in my book, as you cannot count in
base 1.

Can you maybe provide a good starting point for information about hash
marks in base 1? Googling for >hash mark "base 1"< did not give me
satisfactory results (maybe I did not know what to look for).


Cheers
Michael
 
C

Clark S. Cox III

Nope, so I do not know about that.

In base 1, every digit can take values from 0 to base-1=0 and digit
n (counting from 0) represents the multiples of 1^n=1.

Right, which means that there is only one value that a digit can take
(we can call it "0" or we can call it "1", whatever).
So, we can only represent the value 0 which does not make much sense
in terms of counting. If you want to count occurrences of 0, you are
not performing base 1 arithmetics in my book, as you cannot count in
base 1.

Sure you can:
0 -> ""
1 -> "1"
2 -> "11"
3 -> "111"
4 -> "1111"
5 -> "11111"
6 -> "111111"
7 -> "1111111"
8 -> "11111111"
9 -> "111111111"
10-> "1111111111"
 
C

Coos Haak

Op Tue, 31 May 2005 17:20:05 -0400 schreef Clark S. Cox III:
Right, which means that there is only one value that a digit can take
(we can call it "0" or we can call it "1", whatever).


Sure you can:
0 -> ""
1 -> "1"
2 -> "11"
3 -> "111"
4 -> "1111"
5 -> "11111"
6 -> "111111"
7 -> "1111111"
8 -> "11111111"
9 -> "111111111"
10-> "1111111111"

No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.
Read Michael's posting more carefully.
 
B

Ben Pfaff

Coos Haak said:
No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.

1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".
 
K

Keith Thompson

Ben Pfaff said:
1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".

The obvious extrapolation from bases greater than 1 is:

In base N, the allowed digits are 0 through N-1.
The digit in rightmost position denotes the digit value.
The digit in the next position denotes the digit value times N.
The next digit denotes the digit value times N*N.
And so forth.

These rules apply to binary, octal, decimal, hexadecimal, sexagesimal,
etc (ignoring things like C's "0x" prefix).

So unless you invent new special-case rules for base 1, the only
allowed digit is 0, and the only representable number is 0. (I think
Coos Haak was suggesting that the 0 digit could be represented as a
vertical line that bears an uncanny resemblance to the character '1'.
It's a bit of a stretch, but it's the only consistent interpretation
that allows 1111 to be a base-1 number.)

A tally system where 1111 represents 4 is perfectly sensible, but it's
not a based positional system, and calling it "base 1" is
inconsistent.
 
P

pete

Keith said:
These rules apply to binary, octal, decimal, hexadecimal, sexagesimal,
etc (ignoring things like C's "0x" prefix).

So unless you invent new special-case rules for base 1,

There's a little bit about postional number systems
and nonpositional number systems in Knuth,
The Art Of Computer Programming, Volume 2,
section 4.1 Positional Numbers Systems,
page 195.

Base one is a nonpositional number system,
unlike the ones you mentioned.
Each base one digit stands for one.

Whe you hold up three fingers to indicate the number three,
or when you write the Roman numeral for three, that's base one.
 
R

Richard Bos

Ben Pfaff said:
1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".

It's incorrect, but widely (ab-)used that way, in particular in the
crappier kind of introduction to bases.

Richard
 
R

Richard Bos

pete said:
There's a little bit about postional number systems
and nonpositional number systems in Knuth,
The Art Of Computer Programming, Volume 2,
section 4.1 Positional Numbers Systems,
page 195.

Base one is a nonpositional number system,
unlike the ones you mentioned.
Each base one digit stands for one.

No, _tallying_ is a nonpositional number system. Base one is often
abused as a name for tallying, but strictly speaking that's wrong.

Richard
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top