Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf("%f") question

Reply
Thread Tools

printf("%f") question

 
 
ymuntyan@gmail.com
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 3:30 pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
wrote:
> 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?


See http://groups.google.com/group/comp....cc2a95425424f0
It's about a little different thing, passing int as a char, but
I don't think it makes much difference here, because there is no
definitive answer anyway. You can google more, there were much
bigger threads about this stuff (including passing char instead
of int, IIRC).

Yevgen
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-02-2008
wrote:
> On Mar 2, 3:30 pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
> wrote:
>> 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?

>
> See http://groups.google.com/group/comp....cc2a95425424f0
> It's about a little different thing, passing int as a char, but
> I don't think it makes much difference here, because there is no
> definitive answer anyway. You can google more, there were much
> bigger threads about this stuff (including passing char instead
> of int, IIRC).



Does any of these threads reach a conclusion?
 
Reply With Quote
 
 
 
 
ymuntyan@gmail.com
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 4:09 pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
wrote:
> ymunt...@gmail.com wrote:
> > On Mar 2, 3:30 pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
> > wrote:
> >> 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?

>
> > Seehttp://groups.google.com/group/comp.std.c/browse_thread/thread/7af315...
> > It's about a little different thing, passing int as a char, but
> > I don't think it makes much difference here, because there is no
> > definitive answer anyway. You can google more, there were much
> > bigger threads about this stuff (including passing char instead
> > of int, IIRC).

>
> Does any of these threads reach a conclusion?


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

Yevgen
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-02-2008
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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-03-2008
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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-03-2008
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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-03-2008
Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr> wrote:
> 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
 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57