%d format specifier OK for signed char with values from -128 to +127?

Stephen Kairys
Stephen Kairys used Ask the Experts™
on
I have
signed char ch;

and the value in this 1-byte field is a number from -128 to +127, can I safely use the %d format speciifer e.g.
printf("The value is %d\n", ch);

or must I cast it to an int?

Please include the reasoning behind your answer.

Thanks
Steve
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
the argument will be cast to an int whether or not you do it yourself
>> or must I cast it to an int?
You can cast ch to an int, but it is not necessary as the compiler will promote ch implicitly in the printf function.

ch is an integral data type. Since the printf arguments prototype ends with an ellipsis (, ...), then ch will be promoted to an int. (In C99, for example, if a function has a signed char type for an argument, then vendors no longer are required to promote to int.)

From the C standard:
6.3 Conversions
The integer promotions preserve value including sign.
printf() is handy. To convert char to it's equivallent int, we can use %d.
How it works?
While storing any no, like int/float it'll be stored as it's equiallent binary in memry. But in case of char, it stores it's equevallent ASCII.         If ur storing 'A', it's equivallent ASCII is 01000001, While we are printing this value using %c, it'll be ssumed that we want char, So it'll see which char equivallent ASCII is this, So printing 'A'. But if we use %d or %i, it'll assume that we want an int, So this binary will be converted to it's equivallent int, What's this?This is 65. So while using %d, we are getting 65.
 
Hope ths may clear your doubt.
Stephen KairysTechnical Writer - Consultant

Author

Commented:
Subrat2009-
Yes, it does clear my doubt...thank you.
One question, though:

>>But if we use %d or %i, it'll assume that we want an int
Can I assume the same applies to %u?

Thanks
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
The conversion specifiers and their meanings are:
d,i The int argument is converted to signed decimal in the style [-]dddd. The
precision specifies the minimum number of digits to appear; if the value
being converted can be represented in fewer digits, it is expanded with
leading zeros. The default precision is 1. The result of converting a zero
value with a precision of zero is no characters.
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; the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is expanded
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.
Stephen KairysTechnical Writer - Consultant

Author

Commented:
Thanks to all for your info. I intended to close this question and assign points within a day orso..sorry I haven't responded sooner.
Tks again
Steve
Stephen KairysTechnical Writer - Consultant

Author

Commented:
Thank you all for your help - there seems to be a consencus that I'm OK with %d without the cast.

-Steve

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial