Whenever I run this program, It gives output 12592. I couldn't find the logic behind it. It it in C Programming.
#include <stdio.h>
int main()
{
printf("%d",'10');
return 0;
}
Whenever I run this program, It gives output 12592. I couldn't find the logic behind it. It it in C Programming.
#include <stdio.h>
int main()
{
printf("%d",'10');
return 0;
}
'10'
is actually a multi-character constant.
It has a type int
, just like say '1'
, but its actual value is implementation defined.
Quite feasibly its value is '1' * 256 + '0'
which is 12592 on your platform which is, from what I can tell, using ASCII encoding.
As everyone else has said, you are using a multi-character constant, and their explanations tell you WHY you get the result you get, as asked.
If you intended to print out the integer literal value 10 (so far not commented about directly), use
printf("%d",10);
Note there are no single quotes on the second parameter.
For that matter,
printf("10");
OR
printf("%s","10");
(print the string literal) would do just as well.
In C (and C++), the apostrophe character '
is used to form a character constant. Normally, what goes between the apostrophes is one single character like 'A'
, or an escape sequence representing one single character, like '\n'
or '\001'
or '\x01'
.
For historical reasons, it's permissible to put multiple actual characters between the apostrophes, as in 'AB'
, or the case you asked about, '10'
. These are called multi-character character constants.
This is a questionable thing to do. The result is implementation defined, meaning it may do different things under different compilers (although the documentation should tell you what it does). It may also be quite different between C and C++.
Since it's not well-defined what it does, and since there are few good uses for it even if it were well-defined, the usual advice is to just not use multi-character character constants. (And if you don't use them, you don't even have to know what they do.)
Under most C compilers, what you get when you do this is the result of "appending" the two characters in memory -- that is, creating a 16-bit number by putting two 8-bit numbers next to each other. (For now I'm assuming just two characters in the multi-character character constant, although actually there might be even more). It may be easiest to explain this with an example:
#include <stdio.h>
int main()
{
printf("%d (%02x) %d (%02x) -> %d (%02x)\n", 'A', 'A', 'B', 'B', 'AB', 'AB');
printf("%d (%02x) %d (%02x) -> %d (%02x)\n", '1', '1', '0', '0', '10', '10');
}
On my machine this prints
65 (41) 66 (42) -> 16706 (4142)
49 (31) 48 (30) -> 12592 (3130)
(But, side note, on my machine my compiler gives me the message "warning: multi-character character constant", reinforcing the notion that multi-character character constants are usually a bad idea.)
Looking at the second example as bytes in memory; '1' looks like
+------+
+----------+
| 0x31 |
or | 00110001 |
+------+
+----------+
And '10' looks like
+------+------+
+----------+----------+
| 0x31 | 0x30 |
or | 00110001 | 00110000 |
+------+------+
+----------+----------+
Where does that number 12592 come from? It's the decimal representation of the hexadecimal number 0x3130. Or, stated another way, it's 256 × 49 + 48.