printf doubt

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

    printf doubt

    Hi, there is some kind of difference in these two statements?

    float num = 154.87;
    printf("There is : $0.0f",num);


    and


    float num = 154.87;
    printf("There is : $f",num);

  • Richard Heathfield

    #2
    Re: printf doubt

    dutche said:
    Hi, there is some kind of difference in these two statements?
    >
    float num = 154.87;
    printf("There is : $0.0f",num);
    >
    >
    and
    >
    >
    float num = 154.87;
    printf("There is : $f",num);
    Yes. (We will assume that the '$' character is in the execution character
    set.)

    The first of your examples will print:

    There is : $0.0f

    The second will print:

    There is : $f

    These are different outputs.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • dutche

      #3
      Re: printf doubt


      Richard Heathfield escreveu:
      dutche said:
      >
      Hi, there is some kind of difference in these two statements?

      float num = 154.87;
      printf("There is : $0.0f",num);


      and


      float num = 154.87;
      printf("There is : $f",num);
      >
      Yes. (We will assume that the '$' character is in the execution character
      set.)
      >
      The first of your examples will print:
      >
      There is : $0.0f
      >
      The second will print:
      >
      There is : $f
      >
      These are different outputs.
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)


      Sorry I made a mistake :)

      float num = 154.87;
      printf("There is : $%0.0f",num);

      and

      float num = 154.87;
      printf("There is : $%f",num);

      Comment

      • Andrew Poelstra

        #4
        Re: printf doubt

        On 2006-07-11, dutche <dutche@gmail.c omwrote:
        Hi, there is some kind of difference in these two statements?
        >
        float num = 154.87;
        printf("There is : $0.0f",num);
        >
        >
        and
        >
        >
        float num = 154.87;
        printf("There is : $f",num);
        >
        Run them, and you'll note that one will print "There is : $0.0f" and
        other other will print "There is : $f", neither of which will have a
        newline*

        * Actually, on a lot of systems it won't print at all, because you're
        missing a newline.

        You need to read a good beginner's C book.

        --
        Andrew Poelstra <http://www.wpsoftware. net/projects/>
        To email me, use "apoelstra" at the above domain.
        "You people hate mathematics." -- James Harris

        Comment

        • Richard Heathfield

          #5
          Re: printf doubt

          dutche said:

          <snip>
          >
          float num = 154.87;
          printf("There is : $%0.0f",num);
          This will print the value in at least 0 (first 0!) columns but possibly
          more, and with a precision of 0 (second 0) decimal places. The number is
          "rounded to the appropriate number of digits", as the Standard puts it. So
          in this case it'll display 155 (because that's closer than 154).
          and
          >
          float num = 154.87;
          printf("There is : $%f",num);
          This will just print the value, to default precision. On my implementation
          it gives 154.869995 as the result.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • Andrew Poelstra

            #6
            Re: printf doubt

            On 2006-07-11, Andrew Poelstra <apoelstra@loca lhost.localdoma inwrote:
            On 2006-07-11, dutche <dutche@gmail.c omwrote:
            >Hi, there is some kind of difference in these two statements?
            >>
            >float num = 154.87;
            >printf("Ther e is : $0.0f",num);
            >
            You need to read a good beginner's C book.
            >
            Never mind; I thought you confused `$' and `%', but according to your
            other post, you just forgot to type `%', which is an understandable
            mistake (because the characters are right beside each other on a US
            keyboard, and you'd already typed one).

            --
            Andrew Poelstra <http://www.wpsoftware. net/projects/>
            To email me, use "apoelstra" at the above domain.
            "You people hate mathematics." -- James Harris

            Comment

            • Robert Gamble

              #7
              Re: printf doubt

              Richard Heathfield wrote:
              dutche said:
              >
              <snip>

              float num = 154.87;
              printf("There is : $%0.0f",num);
              >
              This will print the value in at least 0 (first 0!) columns but possibly
              more, and with a precision of 0 (second 0) decimal places. The number is
              "rounded to the appropriate number of digits", as the Standard puts it. So
              in this case it'll display 155 (because that's closer than 154).
              >
              and

              float num = 154.87;
              printf("There is : $%f",num);
              >
              This will just print the value, to default precision.
              More precisely, it will print the value as if a precision of 6 had been
              specified (ignoring the fact that no newline is present).

              Robert Gamble

              Comment

              • Fred Kleinschmidt

                #8
                Re: printf doubt


                "dutche" <dutche@gmail.c omwrote in message
                news:1152626834 .778040.179400@ h48g2000cwc.goo glegroups.com.. .
                Hi, there is some kind of difference in these two statements?
                >
                float num = 154.87;
                printf("There is : $0.0f",num);
                >
                >
                and
                >
                >
                float num = 154.87;
                printf("There is : $f",num);
                >
                Yes. The first one will print:
                There is : $0.0f
                and the second will print:
                There is : $f

                And without a newline, it is possible that neither will show up.
                --
                Fred L. Kleinschmidt
                Boeing Associate Technical Fellow
                Technical Architect, Software Reuse Project


                Comment

                • Barry Schwarz

                  #9
                  Re: printf doubt

                  On Tue, 11 Jul 2006 14:14:55 +0000, Richard Heathfield
                  <invalid@invali d.invalidwrote:
                  >dutche said:
                  >
                  >Hi, there is some kind of difference in these two statements?
                  >>
                  >float num = 154.87;
                  >printf("Ther e is : $0.0f",num);
                  >>
                  >>
                  >and
                  >>
                  >>
                  >float num = 154.87;
                  >printf("Ther e is : $f",num);
                  >
                  >Yes. (We will assume that the '$' character is in the execution character
                  >set.)
                  >
                  >The first of your examples will print:
                  >
                  >There is : $0.0f
                  >
                  >The second will print:
                  >
                  >There is : $f
                  >
                  >These are different outputs.
                  Does not the presence of a superfluous argument lead to undefined
                  behavior?


                  Remove del for email

                  Comment

                  • Ben Pfaff

                    #10
                    Re: printf doubt

                    Barry Schwarz <schwarzb@doezl .netwrites:
                    Does not the presence of a superfluous argument lead to undefined
                    behavior?
                    No. From C99 7.19.6.1 "The fprintf function":

                    If the format is exhausted while arguments remain, the
                    excess arguments are evaluated (as always) but are otherwise
                    ignored.
                    --
                    "You call this a *C* question? What the hell are you smoking?" --Kaz

                    Comment

                    • Richard Heathfield

                      #11
                      Re: printf doubt

                      Barry Schwarz said:

                      <snip>
                      >
                      Does not the presence of a superfluous argument lead to undefined
                      behavior?
                      No. The relevant C89 draft wording is:

                      "If the format is exhausted while arguments remain, the excess
                      arguments are evaluated (as always) but are otherwise ignored."

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at above domain (but drop the www, obviously)

                      Comment

                      • Chris Torek

                        #12
                        Re: printf doubt

                        In article <SvWdnfxfb-xyKC7ZRVnyvA@bt .com>
                        Richard Heathfield <invalid@invali d.invalidwrote:
                        >dutche said:
                        >float num = 154.87;
                        >printf("Ther e is : $%0.0f",num);
                        >This will print the value in at least 0 (first 0!) columns ...
                        Aha, a rare Richard Heathfield error! :-)

                        In a printf conversion, the "0" character is a *flag*, not a field
                        width, unless:

                        - you are already working on a field width, or
                        - you are already working on a precision

                        (and possibly if you use various extensions as well). For instance,

                        printf("%040d\n ", 12);

                        prints "00000000000000 000000000000000 00000000012\n", while:

                        printf ("%40d\n", 12);

                        prints " 12\n".

                        A zero flag is allowed with %f conversions, with the obvious meaning
                        (so printing 0.5 with %012.6f produces "00000.5000 00"; note that the
                        field width includes the decimal-point character).
                        >but possibly more, and with a precision of 0 (second 0) decimal
                        >places.
                        Note that, for %f format, if no precision is specified, a default
                        value of 6 is assumed. So:

                        printf("%f\n", val);

                        and:

                        printf("%.6f\n" , val);

                        have identical meanings.
                        --
                        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

                        • Keith Thompson

                          #13
                          Re: printf doubt

                          Andrew Poelstra <apoelstra@loca lhost.localdoma inwrites:
                          On 2006-07-11, dutche <dutche@gmail.c omwrote:
                          >Hi, there is some kind of difference in these two statements?
                          >>
                          >float num = 154.87;
                          >printf("Ther e is : $0.0f",num);
                          >>
                          >>
                          >and
                          >>
                          >>
                          >float num = 154.87;
                          >printf("Ther e is : $f",num);
                          >>
                          >
                          Run them, and you'll note that one will print "There is : $0.0f" and
                          other other will print "There is : $f", neither of which will have a
                          newline*
                          >
                          * Actually, on a lot of systems it won't print at all, because you're
                          missing a newline.
                          Actually, we don't know that he's missing a newline. He's only showed
                          us a couple of small code fragments, not an entire program. If
                          putchar('\n') or equivalent is executed later on, he's fine (at least
                          as far as trailing newlines are concerned).

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

                          • Flash Gordon

                            #14
                            Re: printf doubt

                            Barry Schwarz wrote:
                            On Tue, 11 Jul 2006 14:14:55 +0000, Richard Heathfield
                            <invalid@invali d.invalidwrote:
                            >
                            >dutche said:
                            >>
                            >>Hi, there is some kind of difference in these two statements?
                            >>>
                            >>float num = 154.87;
                            >>printf("The re is : $0.0f",num);
                            <snip>
                            Does not the presence of a superfluous argument lead to undefined
                            behavior?
                            No, you are explicitly allowed to pass extra arguments to printf. The
                            standard states "If the format is exhausted while arguments remain, the
                            excess arguments are evaluated (as always) but are otherwise ignored."
                            --
                            Flash Gordon, living in interesting times.
                            Web site - http://home.flash-gordon.me.uk/
                            comp.lang.c posting guidelines and intro:

                            Comment

                            • Richard Heathfield

                              #15
                              Re: printf doubt

                              Chris Torek said:
                              In article <SvWdnfxfb-xyKC7ZRVnyvA@bt .com>
                              Richard Heathfield <invalid@invali d.invalidwrote:
                              >>dutche said:
                              >>float num = 154.87;
                              >>printf("The re is : $%0.0f",num);
                              >
                              >>This will print the value in at least 0 (first 0!) columns ...
                              >
                              Aha, a rare Richard Heathfield error! :-)
                              I don't know whether to be annoyed or delighted. :-)

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at above domain (but drop the www, obviously)

                              Comment

                              Working...