Program to print decimal value of a char:
#include
int main(void){
char ch = \'AB\';
printf(\"ch is %d\\n\",ch);
}
Why i
char ch = 'AB';
printf("ch is %d\n",ch); // Output "ch is 66"
Why it is printing the decimal value of second character,why not the first character's decimal value?
'AB'
is an int
character constant.
The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. C11 §6.4.4.4 10
Example: Your output may differ.
printf("ch is %d\n",'AB'); // prints "ch is 16706"
16706 is the same value as 0x4142 which is the concatenated value of ASCII A
and B
. A printout out of ch is 16961
(0x4241) or ch is 1111556096
(0x42410000) or others is possible. It is implementation defined behavior.
Assigning 16706
to an char
is either implementation defined behavior or well defined - depending on if char
is signed or unsigned. A common ID result is to assign the lower byte, or 0x42
.
`
printf("ch is %d\n", ch); // prints "ch is 66"
Assigning a value outside the char
range to a char
may raise a warning.
// Example warning
// warning: overflow in implicit constant conversion [-Woverflow]
char ch1 = 'AB';
char ch2 = 16706;
In addition, given the implementation defined nature of such consonant, the below may also warn:
// Example warning
// warning: multi-character character constant [-Wmultichar]
char ch1 = 'AB';
Use of multi-character character constant is limited to few select cases. So few that it is more likely a coding error that a good use.
Iharob is right. 'AB'
being a multi character turns out to be not predictable. If you intend to keep the two characters 'AB'
, I'd recommend declaring this constant as a string char ab[] = "AB";
. The print for a string could be printf("ch is %d\n", ab[0]);
, to have 65
as output.
Because 'AB'
is a multi character constant whose value is implementation defined, whether it's 66
or not is "not predictable" in principle and in practice though predictable is not the same across different implementations.
Normally, you only use a single character in the middle of single quotes. If you use multiple characters,
int
is "not predictable" because it's implementation defined. Of course, given an implementation we hope that a multi character constant does always have the same value.If you have used gcc, then this is what happens according to this source
The compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not. If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.
For example,
'ab'
for a target with an 8-bit char would be interpreted as ‘(int) ((unsigned char) 'a' * 256 + (unsigned char) 'b')
’, and'\234a'
as ‘(int) ((unsigned char) '\234' * 256 + (unsigned char) 'a')
’
C11 $6.4.4.4 (Character constants):
A multi-char always resolves to an int, but that the exact value is “implementation-dependent”. That is, different compilers may resolve the same multi-char to different integers. This is a portability problem, and it is one of the reasons multi-chars are discouraged.
It means int ch = 'AB';
is ok for int
type.But, If it were declared as type char
, the second byte would not be retained.
So use
char ch = 'A';
instead of
char ch = 'AB';
and use %c
format specifier for char
type.
printf("ch is %c\n",ch);
First of all, this program throws a warning like
4:12: warning: multi-character character constant [-Wmultichar]
In function 'int main()':
4:12: warning: overflow in implicit constant conversion [-Woverflow]
If you know the concept of Operator precedence, will help you out why you are getting the decimal value of second character (B). In Assignment operator, precedence priority starts from Right to Left. So in this case, Right most character has more priority and stored into char ch and remaining characters are ignored.
#include<stdio.h>
int main(void){
char ch = 'AB'; // Here B is assigned to 'ch' and A is ignored.
char ch1 = 'ABCDEFG'; // Here G is assigned to 'ch1'
printf("ch is %d\n",ch);
printf("ch1 is '%c'\n",ch1);
}
Output:
ch is 66
ch1 is 'G'
Another example using assignment operator:
#include<stdio.h>
int main(void){
int a = (2,3,4,5,6); // Here 6 is assigned to 'a' and remaining values are ignored.
printf("a is %d\n",a);
}
Output:
a is 6
Kindly go through the below link for operator precedence. http://en.cppreference.com/w/c/language/operator_precedence