Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > data type for decimal number

Reply
Thread Tools

data type for decimal number

 
 
vhanwaribrahim@gmail.com
Guest
Posts: n/a
 
      02-18-2008
On Feb 16, 11:40*pm, (E-Mail Removed) 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/infoce...specifiers.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













 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-18-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      02-18-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-18-2008
On Feb 16, 1:40 pm, (E-Mail Removed) 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-18-2008
(E-Mail Removed) (Richard Tobin) writes:
> In article
> <(E-Mail Removed)>,
> <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      02-18-2008
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?
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-18-2008
Richard Heathfield <(E-Mail Removed)> writes:
> (E-Mail Removed) 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-18-2008
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-18-2008
Keith Thompson said:

> Richard Heathfield <(E-Mail Removed)> writes:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      02-18-2008
On Mon, 18 Feb 2008 17:22:37 +0000, Richard Heathfield wrote:
> Keith Thompson said:
>> Richard Heathfield <(E-Mail Removed)> 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/; [...]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of type Decimal cannot be converted to 1 dimensional array of Decimal Neal Software 0 03-20-2011 03:08 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Conversion from Hexadecimal number into Decimal number dharmdeep@gmail.com C Programming 14 11-16-2006 07:51 PM
convert decimal number in a hexadecimal number ? muss C Programming 13 03-27-2006 08:14 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments