data type for decimal number

Discussion in 'C Programming' started by neha_chhatre@yahoo.co.in, Feb 16, 2008.

  1. Guest

    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
    , Feb 16, 2008
    #1
    1. Advertising

  2. 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
    Richard Heathfield, Feb 16, 2008
    #2
    1. Advertising

  3. <> wrote in message news:
    > 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
    >

    %d, d is for decimal, is the standard printf format specifier for an int.
    There are several modifiers you can pass to control the way the number
    appears, but one thing you cannot do is to lose digits.

    Look up your printf() documentationto get the details.

    You might also want to look at the %f and %g formats for floating-point
    numbers. These too are printed out in decimal, and this case non-significant
    digits can be truncated.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 16, 2008
    #3
  4. On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    <> wrote:

    >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. ;-)
    Robert W Hand, Feb 17, 2008
    #4
  5. Robert W Hand said:

    > On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    > <> wrote:
    >
    >>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
    Richard Heathfield, Feb 18, 2008
    #5
  6. osmium Guest

    "Richard Heathfield" writes:

    > Robert W Hand said:
    >
    >> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    >> <> wrote:
    >>
    >>>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.


    What's your point? The same background is behind every word in every
    language. Bread is what someone thought bread was. You seem to delight in
    bringing up the standard when it pleases you. But if it disagrees with your
    belief system in some tiny way it becomes "just another document in the
    wearisome pile of documents produced by humankind."
    osmium, Feb 18, 2008
    #6
  7. In article <>,
    Richard Heathfield <> wrote:
    >Robert W Hand said:
    >
    >> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    >> <> wrote:
    >>
    >>>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.
    Kenny McCormack, Feb 18, 2008
    #7
  8. osmium said:

    > "Richard Heathfield" writes:
    >
    >> Robert W Hand said:
    >>
    >>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    >>> <> wrote:
    >>>
    >>>>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.

    >
    > What's your point?


    That there is no such thing as a decimal number. The fact that the Standard
    claims otherwise is a flaw in the Standard.

    > You seem to delight in bringing up the standard when it pleases you.


    I bring up the Standard when it's relevant. What pleases me is the
    opportunity to help people learn more about C. C is defined by the
    Standard (although the failure of C99 makes that a rather shaky stance).
    Citing the Standard on occasion is therefore inevitable. But delight? I
    don't think so.

    > But if it disagrees with
    > your belief system in some tiny way it becomes "just another document in
    > the wearisome pile of documents produced by humankind."


    It's an important document, but it's not perfect, and we'd be fools to
    think otherwise.

    --
    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
    Richard Heathfield, Feb 18, 2008
    #8
  9. On 18 Feb 2008 at 3:22, Kenny McCormack wrote:
    > In article <>,
    > Richard Heathfield <> wrote:
    >>Robert W Hand said:
    >>
    >>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
    >>> <> wrote:
    >>>
    >>>>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.


    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.
    Antoninus Twink, Feb 18, 2008
    #9
  10. On 16 Feb, 18:40, wrote:

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


    what is a "decimal number"? If you are working with integers
    then use integers and display the result in decimal. If floating point
    use double then display in decimal.

    Or do you want some form of decimal floating point (I doubt it).


    > also please tell me the syntax for truncating a decimal number


    try the documentaion for printf()


    > please reply as soon as possible


    why?


    --
    Nick Keighley
    Nick Keighley, Feb 18, 2008
    #10
  11. Guest

    On Feb 16, 11:40 pm, 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

    http://publib.boulder.ibm.com/infoc...oc/standlib/ref/printconversionspecifiers.htm







    > 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
    , Feb 18, 2008
    #11
  12. 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.

    > but we know that addresses cant be negative.


    C&V, please.

    > So we should use %u as format specifier.


    No, we should use %p.

    > As you know numbers can be signed or unsigned and the value
    > it can hold depends upon the type we use it.


    Integer types are signed or unsigned. Numbers are values, not types, and
    they are negative, zero, or positive.

    > 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


    Use %f for double if you don't want to nail yourself to the
    widely-unimplemented C99 spec. In C90, %lf invokes undefined behaviour.

    > float - %f


    You can't pass a float as one of the optional parameters of a variadic
    function, no matter how hard you try. It *will* be promoted to double.

    > hex -%x.


    Actually, %x takes an int, not a hex. C doesn't have a hex type.

    > for the rest you can refer this link


    If that's where you got your information from, it isn't worth reading.

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


    There aren't any float format specifiers for printf.

    --
    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
    Richard Heathfield, Feb 18, 2008
    #12
  13. In article <>,
    <> wrote:

    >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 far as C itself is concerned, addresses are not integers. In
    particular, they are not comparable with < and > except within a
    single object. So from C's point of view it doesn't make much sense
    to ask whether an address is negative. And this is true on some
    hardware: who can say whether a pointer represented as a segment
    number and offset is positive or negative?

    Of course, most modern hardware uses a flat address space within
    single programs, and by convention the addresses are non-negative. On
    such systems converting a pointer to a sufficiently large unsigned
    integer type will usually give you the value you expect. If addresses
    happen to fit in an unsigned int, then printing them as %u will
    give you the expected answer.

    But you don't usually need to make such an assumption. The format
    specifier %p exists for just this purpose: it converts a void pointer
    to a textual form that is appropriate for the system.

    You may disagree with the implementation about what form is
    appropriate. You might like addresses displayed in decimal rather
    than hex, for example. In that case you can cast your pointer to an
    appropriately sized int and print it with an appropriate format, but
    you've given up a certain amount of portability.

    -- Richard
    --
    :wq
    Richard Tobin, Feb 18, 2008
    #13
  14. John Bode Guest

    On Feb 16, 1:40 pm, wrote:
    > which is the best format specifier(data type) if i have to work with
    > decimal number.
    >


    For integer values, you use the %d conversion specifier for decimal
    (base-10) representation (with appropriate size modifiers for shorts
    or longs). For floating point values, %f will print out the fraction
    using decimal digits.

    > also please tell me the syntax for truncating a decimal number


    By truncating, I assume you mean cutting off the fractional portion of
    a real number.

    You can assign the floating point number to an integer, like so:

    double f = 3.14159;
    int i = f; // i will equal 3.

    Alternately, you could use the math library functions floor() and
    ceil(), but sign comes into play (i.e., you would use floor() for
    positive values and ceil() for negative values).

    >
    > please reply as soon as possible
    John Bode, Feb 18, 2008
    #14
  15. (Richard Tobin) writes:
    > In article
    > <>,
    > <> wrote:
    >>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 far as C itself is concerned, addresses are not integers. In
    > particular, they are not comparable with < and > except within a
    > single object. So from C's point of view it doesn't make much sense
    > to ask whether an address is negative. And this is true on some
    > hardware: who can say whether a pointer represented as a segment
    > number and offset is positive or negative?
    >
    > Of course, most modern hardware uses a flat address space within
    > single programs, and by convention the addresses are non-negative. On
    > such systems converting a pointer to a sufficiently large unsigned
    > integer type will usually give you the value you expect. If addresses
    > happen to fit in an unsigned int, then printing them as %u will
    > give you the expected answer.


    Yes, *converting* a pointer to an integer type will usually (but not
    always) give you the value you expect. But if you pass a pointer
    directly to printf with a "%u" format:
    int x;
    int *p = &x;
    printf("p = %u\n", p); /* BAD */
    then you're not converting the pointer value; you're passing it as a
    pointer value, and asking printf to *pretend* that it's an unsigned
    int.

    > But you don't usually need to make such an assumption. The format
    > specifier %p exists for just this purpose: it converts a void pointer
    > to a textual form that is appropriate for the system.


    Exactly. And you need to cast (explicitly convert) the pointer to
    void*:

    printf("p = %p\n", (void*)p); /* correct /

    > You may disagree with the implementation about what form is
    > appropriate. You might like addresses displayed in decimal rather
    > than hex, for example. In that case you can cast your pointer to an
    > appropriately sized int and print it with an appropriate format, but
    > you've given up a certain amount of portability.


    In my opinion, this just isn't worth doing. Usually the
    implementation makes a reasonable choice about how to print pointers.
    Picking an appropriate unsigned type isn't easy; unsigned long is very
    often the same size as unsigned long, but it's not guaranteed. But if
    you really feel the need to control the output format, convert the
    pointer to the appropriate type:

    printf("p = %lx\n", (unsigned long)p); /* Not great, but ok */

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 18, 2008
    #15
  16. Mark Bluemel Guest

    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?
    Mark Bluemel, Feb 18, 2008
    #16
  17. Richard Heathfield <> writes:
    > 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*.

    Even this won't necessarily work for function pointers. In fact,
    there is no standard way to print a function pointer value. (You can
    interpret it as an array of unsigned char and print the bytes in hex.)
    Many systems will let you convert a function pointer to void*, but
    it's not guaranteed.

    [...]

    >> int - %d
    >> long - %ld
    >> double - %lf

    >
    > Use %f for double if you don't want to nail yourself to the
    > widely-unimplemented C99 spec. In C90, %lf invokes undefined behaviour.
    >
    >> float - %f

    >
    > You can't pass a float as one of the optional parameters of a variadic
    > function, no matter how hard you try. It *will* be promoted to double.


    Use "%f" for either float or double. Use "%Lf" (note uppercase 'L')
    for long double. As Richard says, "%lf" *might* work for long double,
    but it's not entirely portable (it was introduced in C99).

    Or use "%e" or "%g" if you want a different output format.

    >> hex -%x.

    >
    > Actually, %x takes an int, not a hex. C doesn't have a hex type.


    Actually, %x takes an unsigned int.

    [...]

    When choosing a printf format specifier, you need to consider both the
    type of the argument and the output format you want. The language
    actually doesn't provide format strings for all the possibilities you
    might want; for example, there's no format to print an unsigned
    integer in decimal, or a signed integer in hexadecimal. Usually the
    formats provided by printf are sufficient. If they're not, you can
    program your own conversions.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 18, 2008
    #17
  18. In article <>,
    Keith Thompson <> wrote:

    >> Of course, most modern hardware uses a flat address space within
    >> single programs, and by convention the addresses are non-negative. On
    >> such systems converting a pointer to a sufficiently large unsigned
    >> integer type will usually give you the value you expect. If addresses
    >> happen to fit in an unsigned int, then printing them as %u will
    >> give you the expected answer.


    >Yes, *converting* a pointer to an integer type will usually (but not
    >always) give you the value you expect. But if you pass a pointer
    >directly to printf with a "%u" format:


    By "them", I meant "converted addresses", even though that's not what
    I said.

    >But if
    >you really feel the need to control the output format, convert the
    >pointer to the appropriate type:
    >
    > printf("p = %lx\n", (unsigned long)p); /* Not great, but ok */


    In C99 you may be able to use uintptr_t and PRIuPTR, for example:

    printf("p = %" PRIuPTR "\n", (uintptr_t)p);

    -- Richard
    --
    :wq
    Richard Tobin, Feb 18, 2008
    #18
  19. Keith Thompson said:

    > Richard Heathfield <> writes:
    >> 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
    Richard Heathfield, Feb 18, 2008
    #19
  20. On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
    > Keith Thompson said:
    >> Richard Heathfield <> writes:
    >>> 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/; [...]
    Harald van Dijk, Feb 18, 2008
    #20
    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. makok
    Replies:
    1
    Views:
    6,770
    Anders Hellerup Madsen
    Feb 23, 2004
  2. muss
    Replies:
    13
    Views:
    717
    Vladimir S. Oka
    Mar 27, 2006
  3. Replies:
    14
    Views:
    1,038
    Simon Biber
    Nov 16, 2006
  4. Gilbert Fine
    Replies:
    8
    Views:
    898
    Zentrader
    Aug 1, 2007
  5. Oscar Lok
    Replies:
    1
    Views:
    174
    David Kastrup
    Nov 19, 2006
Loading...

Share This Page