Program to print decimal value of a char:
#include
int main(void){
char ch = \'AB\';
printf(\"ch is %d\\n\",ch);
}
Why i
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
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
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