+ 5

Assigning multiple characters to an integer in C.

#include<stdio.h> void main(void) { int a='dd'; printf("%d ",a); } //OUTPUT : 25700 //For explanation do ping 😊

12th Oct 2018, 9:08 AM
Deepshikhar Bhardwaj
Deepshikhar Bhardwaj - avatar
2 ответов
+ 3
Yes, it happened because of the property of SINGLE QUOTE , which splits it's string into characters in a concatenation fashion. for eg 'deep' is same as d concatenated with e , and e concatenated with other e and other e with p. In above example 'dd' was splited as d d , thus in their binary representation as ( 01100100 01100100 ) and as a whole they sum up to 2^14 + 2^13 + 2^10 + 2^6 + 2^5 + 2^2 = 25700 in decimal. 😊
12th Oct 2018, 10:04 AM
Deepshikhar Bhardwaj
Deepshikhar Bhardwaj - avatar
+ 1
I didn't even know that was valid code, but it turns out it is implementation defined: https://stackoverflow.com/a/26086630/514145 As for the value 25700, I do believe it comes from the fact that the letter 'd' has an ASCII encoding equal to 0x64, in binary 01100100. I think that in the implementation you're using it just interprets the bit pattern corresponding to two 'd' as an integer, indeed the value: 0110010001100100 (the bit pattern above replicated two times) gives 25700 when interpreted as a decimal number
12th Oct 2018, 9:53 AM
fra
fra - avatar