data type for decimal number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neha_chhatre@yahoo.co.in

    data type for decimal number

    which is the best format specifier(data type) if i have to work with
    decimal number.

    also please tell me the syntax for truncating a decimal number

    please reply as soon as possible
  • Richard Heathfield

    #2
    Re: data type for decimal number

    neha_chhatre@ya hoo.co.in said:
    which is the best format specifier(data type) if i have to work with
    decimal number.
    There is no such thing as a "decimal number". Numbers are numbers. But
    presumably you mean that you wish to display a number using decimal
    notation, and your reference to a format specifier suggests that you wish
    to do this using printf.

    If you have an int type, use %d or %i. For short int and long int, apply
    the appropriate modifiers, as listed in your C book (in K&R2 they're on
    page 244). For unsigned int types, use %u rather than %d or %i.

    If you wish to display a floating point type, use %f.
    also please tell me the syntax for truncating a decimal number
    This depends on what you mean by "truncate" and on what type of value you
    wish to truncate (and particularly on whether you want the stored value to
    be truncated, or merely the displayed value).

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Robert W Hand

      #3
      Re: data type for decimal number

      On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
      <rjh@see.sig.in validwrote:
      >There is no such thing as a "decimal number". Numbers are numbers. But
      >presumably you mean that you wish to display a number using decimal
      >notation, and your reference to a format specifier suggests that you wish
      >to do this using printf.
      A tiny point. The Standard does use the term "decimal number" in the
      section on strftime(). Its meaning is exactly what you wrote about
      display, but I would avoid writing that there is no such thing as ....
      You know that there is always an exception. ;-)

      Comment

      • Richard Heathfield

        #4
        Re: data type for decimal number

        Robert W Hand said:
        On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
        <rjh@see.sig.in validwrote:
        >
        >>There is no such thing as a "decimal number". Numbers are numbers. But
        >>presumably you mean that you wish to display a number using decimal
        >>notation, and your reference to a format specifier suggests that you wish
        >>to do this using printf.
        >
        A tiny point. The Standard does use the term "decimal number" in the
        section on strftime(). Its meaning is exactly what you wrote about
        display, but I would avoid writing that there is no such thing as ....
        You know that there is always an exception. ;-)
        The fact that the Standard uses the term "decimal number" does not mean
        there is such a thing as a decimal number. It merely means that the people
        who wrote that part of the Standard think there is such a thing as a
        decimal number.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Kenny McCormack

          #5
          Re: data type for decimal number

          In article <ROCdnX1flqRUfC XanZ2dneKdnZydn Z2d@bt.com>,
          Richard Heathfield <rjh@see.sig.in validwrote:
          >Robert W Hand said:
          >
          >On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
          ><rjh@see.sig.i nvalidwrote:
          >>
          >>>There is no such thing as a "decimal number". Numbers are numbers. But
          >>>presumably you mean that you wish to display a number using decimal
          >>>notation, and your reference to a format specifier suggests that you wish
          >>>to do this using printf.
          >>
          >A tiny point. The Standard does use the term "decimal number" in the
          >section on strftime(). Its meaning is exactly what you wrote about
          >display, but I would avoid writing that there is no such thing as ....
          >You know that there is always an exception. ;-)
          >
          >The fact that the Standard uses the term "decimal number" does not mean
          >there is such a thing as a decimal number. It merely means that the people
          >who wrote that part of the Standard think there is such a thing as a
          >decimal number.
          I will leave it to my esteemed colleague, Mr. Twink, to give this post
          all the respect that it deserves.

          Comment

          • Antoninus Twink

            #6
            Re: data type for decimal number

            On 18 Feb 2008 at 3:22, Kenny McCormack wrote:
            In article <ROCdnX1flqRUfC XanZ2dneKdnZydn Z2d@bt.com>,
            Richard Heathfield <rjh@see.sig.in validwrote:
            >>Robert W Hand said:
            >>
            >>On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
            >><rjh@see.sig. invalidwrote:
            >>>
            >>>>There is no such thing as a "decimal number". Numbers are numbers. But
            >>>>presumabl y you mean that you wish to display a number using decimal
            >>>>notation, and your reference to a format specifier suggests that you wish
            >>>>to do this using printf.
            >>>
            >>A tiny point. The Standard does use the term "decimal number" in the
            >>section on strftime(). Its meaning is exactly what you wrote about
            >>display, but I would avoid writing that there is no such thing as ....
            >>You know that there is always an exception. ;-)
            >>
            >>The fact that the Standard uses the term "decimal number" does not mean
            >>there is such a thing as a decimal number. It merely means that the people
            >>who wrote that part of the Standard think there is such a thing as a
            >>decimal number.
            >
            I will leave it to my esteemed colleague, Mr. Twink, to give this post
            all the respect that it deserves.
            It would be nice to say something funny and cutting, but Heathfield's
            post is so far off into la-la land that it's beyond the power of parody
            to match the absurdity of the post itself... Still, it must be fun in a
            way to be Heathfield, completely unconstrained by reality.

            Comment

            • vhanwaribrahim@gmail.com

              #7
              Re: data type for decimal number

              On Feb 16, 11:40 pm, neha_chha...@ya hoo.co.in wrote:
              which is the best format specifier(data type) if i have to work with
              decimal number.
              >
              The best format specifier for a decimal number depends upon how you
              want
              to use it. If you want to display a normal signed integer , you can
              use "%d"
              unsigned integer "%u".

              Some times the when you print addresses using %d it will show as
              negative,
              but we know that addresses cant be negative. So we should use %u as
              format
              specifier.


              As you know numbers can be signed or unsigned and the value
              it can hold depends upon the type we use it. Also if you want
              large values, you have to use appropriate data types and correspoding
              format specifiers according to that. for eg.

              int - %d
              long - %ld
              double - %lf
              float - %f
              hex -%x.


              for the rest you can refer this link








              also please tell me the syntax for truncating a decimal number
              >
              if you mean to say display after truncating a decimal number,
              then you can use the format specifiers used for float.


              please reply as soon as possible











              Comment

              • Mark Bluemel

                #8
                Re: data type for decimal number

                Keith Thompson wrote:
                ... unsigned long is very
                often the same size as unsigned long, but it's not guaranteed.
                Is this the Schroedinger C compiler we're talking about here?

                Comment

                • Richard Heathfield

                  #9
                  Re: data type for decimal number

                  Keith Thompson said:
                  Richard Heathfield <rjh@see.sig.in validwrites:
                  >vhanwaribrahim@ gmail.com said:
                  >>
                  ><snip>
                  >>
                  >>Some times the when you print addresses using %d it will show as
                  >>negative,
                  >>
                  >Don't print addresses using %d. Addresses are pointer values, so use %p.
                  >
                  Use %p *after* converting the value to void*.
                  Whoops, I forgot to mention that.

                  <snip>
                  >Actually, %x takes an int, not a hex. C doesn't have a hex type.
                  >
                  Actually, %x takes an unsigned int.
                  4.9.6.1 of C89:

                  d, i, o, u, x, X The int argument is converted to signed decimal ( d
                  or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                  hexadecimal notation ( x or X );

                  Sure looks like int to me.

                  <snip>

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • =?UTF-8?q?Harald_van_D=C4=B3k?=

                    #10
                    Re: data type for decimal number

                    On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
                    Keith Thompson said:
                    >Richard Heathfield <rjh@see.sig.in validwrites:
                    >>Actually, %x takes an int, not a hex. C doesn't have a hex type.
                    >>
                    >Actually, %x takes an unsigned int.
                    >
                    4.9.6.1 of C89:
                    >
                    d, i, o, u, x, X The int argument is converted to signed decimal ( d
                    or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                    hexadecimal notation ( x or X );
                    >
                    Sure looks like int to me.
                    Interesting. C99 7.19.6.1p8:

                    o,u,x,X The _unsigned int_ argument is converted to unsigned octal (o),
                    unsigned decimal (u), or unsigned hexadecimal notation (x or X)
                    in the style /dddd/; [...]

                    Comment

                    • Richard Heathfield

                      #11
                      Re: data type for decimal number

                      Harald van D?k said:
                      On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
                      >Keith Thompson said:
                      >>Richard Heathfield <rjh@see.sig.in validwrites:
                      >>>Actually, %x takes an int, not a hex. C doesn't have a hex type.
                      >>>
                      >>Actually, %x takes an unsigned int.
                      >>
                      >4.9.6.1 of C89:
                      >>
                      >d, i, o, u, x, X The int argument is converted to signed decimal ( d
                      > or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                      > hexadecimal notation ( x or X );
                      >>
                      >Sure looks like int to me.
                      >
                      Interesting. C99 7.19.6.1p8:
                      >
                      o,u,x,X The _unsigned int_ argument is converted to unsigned octal (o),
                      unsigned decimal (u), or unsigned hexadecimal notation (x or X)
                      in the style /dddd/; [...]
                      Hmmm. Okay, so that's (perhaps) Yet Another Difference between C89 and C99
                      - and a potentially significant one, since it causes difficulty for those
                      wishing to program in the common subset of the two languages. I say
                      "perhaps" because my source doc for C89 is (pace, C.H.!) a mere draft, and
                      as such is suspect in cases like this. Does anyone have a kosher copy of
                      the "real" C89 in which they could check this out?

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • ymuntyan@gmail.com

                        #12
                        Re: data type for decimal number

                        On Feb 18, 11:46 am, Harald van D©¦k <true...@gmail. comwrote:
                        On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
                        Keith Thompson said:
                        Richard Heathfield <r...@see.sig.i nvalidwrites:
                        >Actually, %x takes an int, not a hex. C doesn't have a hex type.
                        >
                        Actually, %x takes an unsigned int.
                        >
                        4.9.6.1 of C89:
                        >
                        d, i, o, u, x, X The int argument is converted to signed decimal ( d
                        or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                        hexadecimal notation ( x or X );
                        >
                        Sure looks like int to me.
                        >
                        Interesting. C99 7.19.6.1p8:
                        >
                        o,u,x,X The _unsigned int_ argument is converted to unsigned octal (o),
                        unsigned decimal (u), or unsigned hexadecimal notation (x or X)
                        in the style /dddd/; [...]
                        ANSI/ISO 9899-1990
                        American National Standard for Programming Languages - C

                        7.9.6.1 The fprintf function

                        o, u, , x, X The unsigned int argument...

                        Comment

                        • Richard Tobin

                          #13
                          Re: data type for decimal number

                          In article <7J-dnX6UeKWCWiTanZ 2dnUVZ8sDinZ2d@ bt.com>,
                          Richard Heathfield <rjh@see.sig.in validwrote:
                          >Hmmm. Okay, so that's (perhaps) Yet Another Difference between C89 and C99
                          >- and a potentially significant one, since it causes difficulty for those
                          >wishing to program in the common subset of the two languages. I say
                          >"perhaps" because my source doc for C89 is (pace, C.H.!) a mere draft, and
                          >as such is suspect in cases like this. Does anyone have a kosher copy of
                          >the "real" C89 in which they could check this out?
                          My copies of C89 (which are BSI standard BS EN 29899:1993, and the BSI
                          copy of the ISO draft copied from ANSI distributed in 1989-90 for
                          comments), both specify unsigned int for o,u,x,X. A 1984 draft has
                          wording similar to yours.

                          -- Richard
                          --
                          :wq

                          Comment

                          • ymuntyan@gmail.com

                            #14
                            Re: data type for decimal number

                            On Feb 18, 12:22 pm, ymunt...@gmail. com wrote:
                            On Feb 18, 11:46 am, Harald van D©¦k <true...@gmail. comwrote:
                            >
                            >
                            >
                            On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
                            Keith Thompson said:
                            >Richard Heathfield <r...@see.sig.i nvalidwrites:
                            >>Actually, %x takes an int, not a hex. C doesn't have a hex type.
                            >
                            >Actually, %x takes an unsigned int.
                            >
                            4.9.6.1 of C89:
                            >
                            d, i, o, u, x, X The int argument is converted to signed decimal ( d
                            or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                            hexadecimal notation ( x or X );
                            >
                            Sure looks like int to me.
                            >
                            Interesting. C99 7.19.6.1p8:
                            >
                            o,u,x,X The _unsigned int_ argument is converted to unsigned octal (o),
                            unsigned decimal (u), or unsigned hexadecimal notation (x or X)
                            in the style /dddd/; [...]
                            >
                            ANSI/ISO 9899-1990
                            American National Standard for Programming Languages - C
                            >
                            7.9.6.1 The fprintf function
                            >
                            o, u, , x, X The unsigned int argument...
                            Another interesting thing:

                            What the heck?

                            Comment

                            • Walter Roberson

                              #15
                              Re: data type for decimal number

                              In article <7J-dnX6UeKWCWiTanZ 2dnUVZ8sDinZ2d@ bt.com>,
                              Richard Heathfield <rjh@see.sig.in validwrote:
                              >Harald van D?k said:
                              >On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
                              >>Keith Thompson said:
                              >>>Richard Heathfield <rjh@see.sig.in validwrites:
                              >>>>Actually, %x takes an int, not a hex. C doesn't have a hex type.
                              >>>Actually, %x takes an unsigned int.
                              >>4.9.6.1 of C89:
                              >>d, i, o, u, x, X The int argument is converted to signed decimal ( d
                              >> or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
                              >> hexadecimal notation ( x or X );
                              >>Sure looks like int to me.
                              >Interesting. C99 7.19.6.1p8:
                              >o,u,x,X The _unsigned int_ argument is converted to unsigned octal (o),
                              > unsigned decimal (u), or unsigned hexadecimal notation (x or X)
                              > in the style /dddd/; [...]
                              >Hmmm. Okay, so that's (perhaps) Yet Another Difference between C89 and C99
                              >- and a potentially significant one, since it causes difficulty for those
                              >wishing to program in the common subset of the two languages. I say
                              >"perhaps" because my source doc for C89 is (pace, C.H.!) a mere draft, and
                              >as such is suspect in cases like this. Does anyone have a kosher copy of
                              >the "real" C89 in which they could check this out?
                              I have the official ANSI C89 hard-copy publication. It describes
                              the integer formats in two related paragraphs, the first of which
                              is a summary for all of the formats and uses "int", and the second
                              of which is specific to o,u,x,X and uses "unsigned int".

                              I consider the second of the paragraphs to be a refinement of the first,
                              with the first giving the overview and the second the specifics. Thus
                              I see no contradiction between versions: as far as I am concerned, both
                              C89 and C99 specify that %x and %X require unsigned int.
                              --
                              This is a Usenet signature block. Please do not quote it when replying
                              to one of my postings.

                              Comment

                              Working...