Stephen Kairys
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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
Tks again
Steve
ASKER
Thank you all for your help - there seems to be a consencus that I'm OK with %d without the cast.
-Steve
-Steve
ASKER
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