Link to home
Start Free TrialLog in
Avatar of Stephen Kairys
Stephen KairysFlag for United States of America

asked on

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

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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stephen Kairys

ASKER

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
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.
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
Thank you all for your help - there seems to be a consencus that I'm OK with %d without the cast.

-Steve