data type for decimal number

N

neha_chhatre

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
 
R

Richard Heathfield

(e-mail address removed) 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).
 
M

Malcolm McLean

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

Robert W Hand

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. ;-)
 
R

Richard Heathfield

Robert W Hand said:
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.
 
O

osmium

Richard Heathfield said:
Robert W Hand said:


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

Kenny McCormack

Robert W Hand said:


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

Richard Heathfield

osmium said:
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.
 
A

Antoninus Twink

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

Nick Keighley

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

vhanwaribrahim

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

Richard Heathfield

(e-mail address removed) said:

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.

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

Richard Tobin

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
 
J

John Bode

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

Keith Thompson

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 */
 
M

Mark Bluemel

Keith said:
... 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?
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said:



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.

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


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

Richard Tobin

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.
[/QUOTE]
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
 
R

Richard Heathfield

Keith Thompson said:
Use %p *after* converting the value to void*.

Whoops, I forgot to mention that.

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

Harald van Dijk

Keith Thompson said:

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/; [...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top