casting for varargs required? 
Author Message
 casting for varargs required?

Consider the following:

    unsigned char s[3];
    unsigned char c = 'A';

    sprintf(s, "%02x", c);

sprintf(3) says that the 'x' conversion specifier takes an unsigned int
argument.

The above seems to work on my system, but my guess is that this is just
luck because it's little endian.  In order to be correct, must I cast
c to an unsigned int?  Or is the compiler helping me out here.

The man page also documents an 'hh' modifier, but H&S doesn't mention it.
Is that a non-standard modifier?

thanks!
/fc



Fri, 25 Nov 2005 14:01:23 GMT  
 casting for varargs required?

Quote:
> Consider the following:

>     unsigned char s[3];
>     unsigned char c = 'A';

>     sprintf(s, "%02x", c);

> sprintf(3) says that the 'x' conversion specifier takes an unsigned int
> argument.

Correct.

Quote:
> The above seems to work on my system, but my guess is that this is just
> luck because it's little endian.  In order to be correct, must I cast
> c to an unsigned int?  Or is the compiler helping me out here.

The compiler will perform an implicit conversion to 'int'. You really want an
'unsigned int'.

I suggest:

   sprintf(s, "%02x", (unsigned) c);

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Fri, 25 Nov 2005 18:18:28 GMT  
 casting for varargs required?
# Consider the following:
#
#     unsigned char s[3];
#     unsigned char c = 'A';
#    
#     sprintf(s, "%02x", c);
#
# sprintf(3) says that the 'x' conversion specifier takes an unsigned int
# argument.
#
# The above seems to work on my system, but my guess is that this is just
# luck because it's little endian.  In order to be correct, must I cast
# c to an unsigned int?  Or is the compiler helping me out here.

Nonnegative signed integers and unsigned integers have the same bit patterns
on any machine you're likely to use.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.



Fri, 25 Nov 2005 19:16:58 GMT  
 casting for varargs required?


Quote:
> >     unsigned char s[3];
> >     unsigned char c = 'A';

> >     sprintf(s, "%02x", c);

> > sprintf(3) says that the 'x' conversion specifier takes an unsigned int
> > argument.

> Correct.

> > The above seems to work on my system, but my guess is that this is just
> > luck because it's little endian.  In order to be correct, must I cast
> > c to an unsigned int?  Or is the compiler helping me out here.

> The compiler will perform an implicit conversion to 'int'. You really want
an
> 'unsigned int'.

> I suggest:

>    sprintf(s, "%02x", (unsigned) c);

Hmm, I'd say it won't make a difference if an unsigned char is cast to an
int or an unsigned int, what could be a problem then?

unsigned char c = ~0;

 sprintf(s, "%02x", c);



Fri, 25 Nov 2005 20:06:42 GMT  
 casting for varargs required?

Quote:

>> Consider the following:

>>     unsigned char s[3];
>>     unsigned char c = 'A';

>>     sprintf(s, "%02x", c);
> The compiler will perform an implicit conversion to 'int'. You
> really want an 'unsigned int'.

Can you explain why an implicit conversion to 'int' is done?  What if
c were a 'long'?  What if my format arg is %c and I really want a char?
(Although, I guess %c with a char arg is the same as %c with an int arg.)

/fc



Fri, 25 Nov 2005 20:49:32 GMT  
 casting for varargs required?


Quote:


>>> Consider the following:

>>>     unsigned char s[3];
>>>     unsigned char c = 'A';

>>>     sprintf(s, "%02x", c);

>> The compiler will perform an implicit conversion to 'int'. You
>> really want an 'unsigned int'.

>Can you explain why an implicit conversion to 'int' is done?  What if
>c were a 'long'?  What if my format arg is %c and I really want a char?
>(Although, I guess %c with a char arg is the same as %c with an int arg.)

Check out the section(s) on "promotions" in your texts.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


Fri, 25 Nov 2005 20:54:53 GMT  
 casting for varargs required?


# >
# >> Consider the following:
# >>
# >>     unsigned char s[3];
# >>     unsigned char c = 'A';
# >>    
# >>     sprintf(s, "%02x", c);
#
# > The compiler will perform an implicit conversion to 'int'. You
# > really want an 'unsigned int'.
#
# Can you explain why an implicit conversion to 'int' is done?  What if
# c were a 'long'?  What if my format arg is %c and I really want a char?
# (Although, I guess %c with a char arg is the same as %c with an int arg.)

In a var args list, or to old style function (like 'int f(x) int x; {...}'),
floats are converted to doubles and integers shorter than int are converted
signed or unsigned int. %c takes an int argument; if you specify a char, it
is widenned to full int.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.



Fri, 25 Nov 2005 21:35:27 GMT  
 casting for varargs required?

wrote in comp.lang.c:

Quote:

> # Consider the following:
> #
> #     unsigned char s[3];
> #     unsigned char c = 'A';
> #    
> #     sprintf(s, "%02x", c);
> #
> # sprintf(3) says that the 'x' conversion specifier takes an unsigned int
> # argument.
> #
> # The above seems to work on my system, but my guess is that this is just
> # luck because it's little endian.  In order to be correct, must I cast
> # c to an unsigned int?  Or is the compiler helping me out here.

> Nonnegative signed integers and unsigned integers have the same bit patterns
> on any machine you're likely to use.

The bit pattern for any unsigned integer type containing a value
within the range of the corresponding signed integer type is required
to have the same bit pattern on any platform with a C compiler.  It's
required by the standard.

What is not required, but likely to be true on any machine that anyone
is likely to use, is passing in the same way.  There is nothing
forbidding a conforming implementation from passing signed ints in
machine register q, and unsigned ints in machine register z.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 26 Nov 2005 11:56:56 GMT  
 casting for varargs required?
# What is not required, but likely to be true on any machine that anyone
# is likely to use, is passing in the same way.  There is nothing
# forbidding a conforming implementation from passing signed ints in
# machine register q, and unsigned ints in machine register z.

Try putting that on the market.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Quit killing people. That's high profile.



Sat, 26 Nov 2005 13:35:04 GMT  
 casting for varargs required?
Groovy hepcat Emmanuel Delahaye was jivin' on 9 Jun 2003 10:18:28 GMT
in comp.lang.c.
Re: casting for varargs required?'s a cool scene! Dig it!

Quote:

>>     unsigned char s[3];
>>     unsigned char c = 'A';

>>     sprintf(s, "%02x", c);

>> sprintf(3) says that the 'x' conversion specifier takes an unsigned int
>> argument.
>> The above seems to work on my system, but my guess is that this is just
>> luck because it's little endian.  In order to be correct, must I cast
>> c to an unsigned int?  Or is the compiler helping me out here.

>The compiler will perform an implicit conversion to 'int'. You really want an
>'unsigned int'.

  Or unsigned int, if int cannot represent every possible value of
unsigned char (eg., if sizeof(unsigned char) == sizeof(unsigned int)).

Quote:
>I suggest:

>   sprintf(s, "%02x", (unsigned) c);

  Good suggestion!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?



Mon, 28 Nov 2005 11:07:39 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Prototype required for varargs functions?

2. Newbie very puzzled - no cast required??

3. Help with structure element casting required

4. varargs -> varargs mystery

5. using varargs function to call another varargs function

6. VARARGS function calling VARARGS function

7. Where is vsscanf ? (Was: Calling varargs from varargs)

8. varargs fn calling varargs fn?

9. Why is casting required here?

10. CStatic+CBitmap "A required was required"

11. old style casts vs new style casts

12. casting of struct element void pointer casting

 

 
Powered by phpBB® Forum Software