Formatted IO; %d or %i?

Discussion in 'C Programming' started by August Karlstrom, May 28, 2005.

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

    Regards,
    August
     
    August Karlstrom, May 28, 2005
    #1
    1. Advertising

  2. August Karlstrom

    Chris Torek Guest

    In article <9g0me.25778$>
    August Karlstrom <> wrote:
    >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.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, May 28, 2005
    #2
    1. Advertising

  3. August Karlstrom

    Mark Guest

    "Chris Torek" <> wrote in message
    news:...
    > base of 10 (%d) or 0 (%i).


    How does one count in base 0?
     
    Mark, May 31, 2005
    #3
  4. August Karlstrom

    Chris Torek Guest

    >"Chris Torek" <> wrote in message
    >news:...
    >> base of 10 (%d) or 0 (%i).


    In article <A10ne.10398$>
    Mark <> wrote:
    >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.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, May 31, 2005
    #4
  5. August Karlstrom

    Michael Mair Guest

    Mark wrote:
    > "Chris Torek" <> wrote in message
    > news:...
    >
    >>base of 10 (%d) or 0 (%i).

    >
    > 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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 31, 2005
    #5
  6. August Karlstrom

    Guest

    Michael Mair <> wrote:
    >
    > 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
     
    , May 31, 2005
    #6
  7. August Karlstrom

    Michael Mair Guest

    wrote:
    > Michael Mair <> wrote:
    >
    >>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?


    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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 31, 2005
    #7
  8. On 2005-05-31 16:51:11 -0400, Michael Mair <> said:

    > wrote:
    >> Michael Mair <> wrote:
    >>
    >>> 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?

    >
    > 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"


    --
    Clark S. Cox, III
     
    Clark S. Cox III, May 31, 2005
    #8
  9. August Karlstrom

    Coos Haak Guest

    Op Tue, 31 May 2005 17:20:05 -0400 schreef Clark S. Cox III:

    > On 2005-05-31 16:51:11 -0400, Michael Mair <> said:
    >
    >> wrote:
    >>> Michael Mair <> wrote:
    >>>
    >>>> 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?

    >>
    >> 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"


    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.

    --
    Coos
     
    Coos Haak, Jun 1, 2005
    #9
  10. August Karlstrom

    Ben Pfaff Guest

    Coos Haak <> writes:

    > 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".
    --
    "Given that computing power increases exponentially with time,
    algorithms with exponential or better O-notations
    are actually linear with a large constant."
    --Mike Lee
     
    Ben Pfaff, Jun 1, 2005
    #10
  11. Ben Pfaff <> writes:
    > Coos Haak <> writes:
    >
    >> 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".


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 1, 2005
    #11
  12. August Karlstrom

    pete Guest

    Keith Thompson wrote:

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

    --
    pete
     
    pete, Jun 1, 2005
    #12
  13. August Karlstrom

    Richard Bos Guest

    Ben Pfaff <> wrote:

    > Coos Haak <> writes:
    >
    > > 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".


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

    Richard
     
    Richard Bos, Jun 1, 2005
    #13
  14. August Karlstrom

    Richard Bos Guest

    pete <> wrote:

    > Keith Thompson wrote:
    >
    > > 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.


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

    Richard
     
    Richard Bos, Jun 1, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert V
    Replies:
    1
    Views:
    1,029
    Jim Rendant
    Jul 19, 2003
  2. leaf

    formatted data

    leaf, Feb 5, 2006, in forum: VHDL
    Replies:
    8
    Views:
    740
  3. Stuart Woodard

    Server-side Printing With Formatted Text

    Stuart Woodard, Aug 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    329
    Stuart Woodard
    Aug 3, 2003
  4. Paul Turley

    Sending HTML formatted mail using CDONTS

    Paul Turley, Aug 12, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    350
    Parker Zhang [MSFT]
    Aug 12, 2003
  5. Ayende Rahien

    Correct way to add formatted HTML?

    Ayende Rahien, Dec 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    524
    Steve C. Orr [MVP, MCSD]
    Dec 18, 2003
Loading...

Share This Page