Formatted IO; %d or %i?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • August Karlstrom

    Formatted IO; %d or %i?

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

    Regards,
    August
  • Chris Torek

    #2
    Re: Formatted IO; %d or %i?

    In article <9g0me.25778$d5 .175773@newsb.t elia.net>
    August Karlstrom <fusionfive@com hem.se> wrote:[color=blue]
    >Is there any difference between `%d' and `%i' in formatted IO? Which one
    >is preferred?[/color]

    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.

    Comment

    • Mark

      #3
      Re: Formatted IO; %d or %i?

      "Chris Torek" <nospam@torek.n et> wrote in message
      news:d7a4kq01sh v@news2.newsguy .com...[color=blue]
      > base of 10 (%d) or 0 (%i).[/color]

      How does one count in base 0?


      Comment

      • Michael Mair

        #4
        Re: Formatted IO; %d or %i?

        Mark wrote:[color=blue]
        > "Chris Torek" <nospam@torek.n et> wrote in message
        > news:d7a4kq01sh v@news2.newsguy .com...
        >[color=green]
        >>base of 10 (%d) or 0 (%i).[/color]
        >
        > How does one count in base 0?[/color]

        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.

        Comment

        • Chris Torek

          #5
          Re: Formatted IO; %d or %i?

          >"Chris Torek" <nospam@torek.n et> wrote in message[color=blue]
          >news:d7a4kq01s hv@news2.newsgu y.com...[color=green]
          >> base of 10 (%d) or 0 (%i).[/color][/color]

          In article <A10ne.10398$5I 5.852151@newsho g.newsread.com>
          Mark <sober@localbar .com> wrote:[color=blue]
          >How does one count in base 0?[/color]

          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.

          Comment

          • lawrence.jones@ugs.com

            #6
            Re: Formatted IO; %d or %i?

            Michael Mair <Michael.Mair@i nvalid.invalid> wrote:[color=blue]
            >
            > BTW: You cannot work sensibly with base 1[/color]

            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

            Comment

            • Michael Mair

              #7
              Re: Formatted IO; %d or %i?

              lawrence.jones@ ugs.com wrote:[color=blue]
              > Michael Mair <Michael.Mair@i nvalid.invalid> wrote:
              >[color=green]
              >>BTW: You cannot work sensibly with base 1[/color]
              >
              > You can if all you need to do is count -- have you never used hash/tally
              > marks?[/color]

              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.

              Comment

              • Clark S. Cox III

                #8
                Re: Formatted IO; %d or %i?

                On 2005-05-31 16:51:11 -0400, Michael Mair <Michael.Mair@i nvalid.invalid> said:
                [color=blue]
                > lawrence.jones@ ugs.com wrote:[color=green]
                >> Michael Mair <Michael.Mair@i nvalid.invalid> wrote:
                >>[color=darkred]
                >>> BTW: You cannot work sensibly with base 1[/color]
                >>
                >> You can if all you need to do is count -- have you never used hash/tally
                >> marks?[/color]
                >
                > 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.[/color]

                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).
                [color=blue]
                > 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.[/color]

                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
                clarkcox3@gmail .com

                Comment

                • Coos Haak

                  #9
                  Re: Formatted IO; %d or %i?

                  Op Tue, 31 May 2005 17:20:05 -0400 schreef Clark S. Cox III:
                  [color=blue]
                  > On 2005-05-31 16:51:11 -0400, Michael Mair <Michael.Mair@i nvalid.invalid> said:
                  >[color=green]
                  >> lawrence.jones@ ugs.com wrote:[color=darkred]
                  >>> Michael Mair <Michael.Mair@i nvalid.invalid> 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?[/color]
                  >>
                  >> 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.[/color]
                  >
                  > 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).
                  >[color=green]
                  >> 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.[/color]
                  >
                  > Sure you can:
                  > 0 -> ""
                  > 1 -> "1"
                  > 2 -> "11"
                  > 3 -> "111"
                  > 4 -> "1111"
                  > 5 -> "11111"
                  > 6 -> "111111"
                  > 7 -> "1111111"
                  > 8 -> "11111111"
                  > 9 -> "111111111"
                  > 10-> "1111111111 "[/color]

                  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

                  Comment

                  • Ben Pfaff

                    #10
                    Re: Formatted IO; %d or %i?

                    Coos Haak <chforth_HAAL_D IT_WEG_@hccnet. nl> writes:
                    [color=blue]
                    > No, if you call your digit '1', 1+1+1+1 still is 1.
                    > So you can't count with base 1.[/color]

                    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

                    Comment

                    • Keith Thompson

                      #11
                      Re: Formatted IO; %d or %i?

                      Ben Pfaff <blp@cs.stanfor d.edu> writes:[color=blue]
                      > Coos Haak <chforth_HAAL_D IT_WEG_@hccnet. nl> writes:
                      >[color=green]
                      >> No, if you call your digit '1', 1+1+1+1 still is 1.
                      >> So you can't count with base 1.[/color]
                      >
                      > 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".[/color]

                      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_Keit h) kst-u@mib.org <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.

                      Comment

                      • pete

                        #12
                        Re: Formatted IO; %d or %i?

                        Keith Thompson wrote:
                        [color=blue]
                        > 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,[/color]

                        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

                        Comment

                        • Richard Bos

                          #13
                          Re: Formatted IO; %d or %i?

                          Ben Pfaff <blp@cs.stanfor d.edu> wrote:
                          [color=blue]
                          > Coos Haak <chforth_HAAL_D IT_WEG_@hccnet. nl> writes:
                          >[color=green]
                          > > No, if you call your digit '1', 1+1+1+1 still is 1.
                          > > So you can't count with base 1.[/color]
                          >
                          > 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".[/color]

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

                          Richard

                          Comment

                          • Richard Bos

                            #14
                            Re: Formatted IO; %d or %i?

                            pete <pfiland@mindsp ring.com> wrote:
                            [color=blue]
                            > Keith Thompson wrote:
                            >[color=green]
                            > > 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,[/color]
                            >
                            > 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.[/color]

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

                            Richard

                            Comment

                            Working...