472,958 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

printf("%f") question

If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?
Mar 2 '08 #1
13 6412
Clarification: The question is about C90/C95.

Ioannis Vranos wrote:
If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?
Mar 2 '08 #2
On Sun, 02 Mar 2008 15:16:11 +0200, Ioannis Vranos wrote:
If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?
Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.
Mar 2 '08 #3
Harald van Dijk wrote:
Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.

I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);
Mar 2 '08 #4
On Sun, 02 Mar 2008 15:39:42 +0200, Ioannis Vranos wrote:
Harald van Dijk wrote:
>Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.

I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);
On most systems, unsigned char will be promoted to signed int, which
should be printed using %d. You can usually -- probably on all existing
implementations -- get away with printing a signed int using %u, or an
unsigned int using %d, so long as the value is within the common range,
but it's not correct.
Mar 2 '08 #5
Ioannis Vranos wrote:
If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?
x will be promoted to a double, following the normal promotion rules for
arguments. The cast is unnecessary.
Mar 2 '08 #6
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 02 Mar 2008 15:39:42 +0200, Ioannis Vranos wrote:
>Harald van Dijk wrote:
>>Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.

I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);

On most systems, unsigned char will be promoted to signed int, which
should be printed using %d. You can usually -- probably on all existing
implementations -- get away with printing a signed int using %u, or an
unsigned int using %d, so long as the value is within the common range,
but it's not correct.
signed char definitely promotes to (signed) int, so the "%d" is ok.

The "%hu" should be "%hhu" ("%hu" is for unsigned short). The "%hhu"
format prints a value of type unsigned char; "the argument will have
been promoted according to the integer promotions, but its value shall
be converted to signed char or unsigned char before printing" (C99
7.19.6.1p7). That's exactly what "%hhu" is for.

Harald is correct about the "%u" format; "%hhu" is better than "%u",
even though "%u" is very likely to work as expected.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 2 '08 #7
Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>On Sun, 02 Mar 2008 15:39:42 +0200, Ioannis Vranos wrote:
>>Harald van Dijk wrote:
Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.
I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);
On most systems, unsigned char will be promoted to signed int, which
should be printed using %d. You can usually -- probably on all existing
implementations -- get away with printing a signed int using %u, or an
unsigned int using %d, so long as the value is within the common range,
but it's not correct.

signed char definitely promotes to (signed) int, so the "%d" is ok.

The "%hu" should be "%hhu" ("%hu" is for unsigned short). The "%hhu"
format prints a value of type unsigned char; "the argument will have
been promoted according to the integer promotions, but its value shall
be converted to signed char or unsigned char before printing" (C99
7.19.6.1p7). That's exactly what "%hhu" is for.

Harald is correct about the "%u" format; "%hhu" is better than "%u",
even though "%u" is very likely to work as expected.

Thanks for the answer, I forgot to mention that I am talking about
C90/C95 here.
Mar 2 '08 #8
Ioannis Vranos wrote:
Keith Thompson wrote:
>Harald van Dijk <tr*****@gmail.comwrites:
>>On Sun, 02 Mar 2008 15:39:42 +0200, Ioannis Vranos wrote:
Harald van Dijk wrote:
Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.
I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);
On most systems, unsigned char will be promoted to signed int, which
should be printed using %d. You can usually -- probably on all
existing implementations -- get away with printing a signed int
using %u, or an unsigned int using %d, so long as the value is
within the common range, but it's not correct.

signed char definitely promotes to (signed) int, so the "%d" is ok.

The "%hu" should be "%hhu" ("%hu" is for unsigned short). The "%hhu"
format prints a value of type unsigned char; "the argument will have
been promoted according to the integer promotions, but its value
shall be converted to signed char or unsigned char before printing"
(C99
7.19.6.1p7). That's exactly what "%hhu" is for.

Harald is correct about the "%u" format; "%hhu" is better than "%u",
even though "%u" is very likely to work as expected.


Thanks for the answer, I forgot to mention that I am talking about
C90/C95 here.
In which case you'll have to use %u.

Mar 2 '08 #9
santosh wrote:
>
>Thanks for the answer, I forgot to mention that I am talking about
C90/C95 here.

In which case you'll have to use %u.

Thanks for the answer. So, if I want to print the numeric value of an
unsigned char , can I use printf("%u") without a cast?

Mar 2 '08 #10
ym******@gmail.com wrote:
>
Are you kidding? A resolved DR would be a conclusion, otherwise
you read what people say and either agree or disagree. I personally
stick to the following: it's UB, but works fine here (where "here"
is "everywhere").

Well, I think casting the value to the expected type by the printf(), is
the safest solution.
Mar 2 '08 #11
On Mar 3, 8:30*am, Ioannis Vranos wrote:
... So, if I want to print the numeric value of an
unsigned char , can I use printf("%u") without a
cast?
Certainly...

unsigned char uc;
printf("%u", 0u + uc);

--
Peter
Mar 3 '08 #12
Peter Nilsson wrote:
On Mar 3, 8:30 am, Ioannis Vranos wrote:
>... So, if I want to print the numeric value of an
unsigned char , can I use printf("%u") without a
cast?

Certainly...

unsigned char uc;
printf("%u", 0u + uc);

I mean with the unsigned char variable alone, not with an interaction
with an unsigned int constant or variable.
Mar 3 '08 #13
Ioannis Vranos <ivra...@nospam.no.spamfreemail.grwrote:
Peter Nilsson wrote:
On Mar 3, 8:30 am, Ioannis Vranos wrote:
... So, if I want to print the numeric value of an
unsigned char , can I use printf("%u") without a
cast?
* unsigned char uc;
* printf("%u", 0u + uc);

I mean with the unsigned char variable alone,
It's my opinion that the literal wording of the standard
differs from the stated intent for both C90/95 and C99.
The intent is that a common value to both should work as
an argument to either signed/unsigned version of an
integer rank.

However, the standard clearly ambiguates this, if not
actually precludes it, in many places. For instance,
va_arg() is required to work this way, but fprintf isn't
required to use va_arg().

The point is, if you want to be pedantic, then play it
safe.
not with an interaction with an unsigned int constant
or variable.
The reason I throw in a 0u is precisely to avoid
ambiguities around the promotion of unsigned char.
A cast would be sufficient, and more idiomatic, but
it's ugly IMHO.

--
Peter
Mar 3 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Chris Potter | last post by:
Hello everyone. I am taking my first course in C and in one of my assignments i need to print out an array that could have anywhere from 0 to 100 positive integers in it (a negative integer is...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
13
by: Dave win | last post by:
howdy.... plz take a look at the following codes, and tell me the reason. 1 #define swap(a,b) a=a^b;b=b^a;a=a^b 2 3 int main(void){ 4 register int a=4; 5 register int b=5;...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
3
by: Ken | last post by:
Hi all, I want to printf a sentence with a semicolon, for examples: printf(" I like C language; You like C++ language."); But C compiler alway identify the semicolon as a end of a sentence and...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
9
by: hstagni | last post by:
I tried to use fseek in a file opened for writing: ------ BEGIN ------- int main() { char c; FILE *fp; fp=fopen("texto", "wb"); putc('a', fp); putc('b', fp);
3
by: DrVitoti | last post by:
On that program the compiler says "parse error" on line 8, 10, 12 and 21, it also says "too many arguments" on lines 10, 12 and finally it says "at this port in" on lines 13, 14, 20 . How could I...
27
by: sophia | last post by:
Dear all, why in the following program #include<stdio.h> #include<stdlib.h> int main(void) {
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.