In my following code:
main(){
int c;
char c1='0';
int x=9-c1;
int y='9'-c1;
}
Now in this program I'm getting value of x as some arbitrary value, but the value of y is 0, which is the value that I expect. Why this difference?
In my following code:
main(){
int c;
char c1='0';
int x=9-c1;
int y='9'-c1;
}
Now in this program I'm getting value of x as some arbitrary value, but the value of y is 0, which is the value that I expect. Why this difference?
Here is a good explanation. Just compile it and run:
#include <stdio.h>
int main(){
int c;
char c1='0';
int x=9-c1;
int y='9'-c1;
printf("--Code and Explanation--\n");
printf("int c;\n");
printf("char c1='0';\n");
printf("int x=9-c1;\n");
printf("int y='9'-c1;\n");
printf("c1 as char '0' has decimal value: %d\n", c1);
printf("decimal 9 - decimal %d or c1 = %d or x\n", c1, x);
printf("char '9' has decimal value %d - decimal %d or c1 = %d\n", '9', c1, y);
printf("Your Welcome :)\n");
return 0;
}
1st char
are integers.
2nd char
s might have a printable representation or output controlling function (like for ASCII: TAB
, CR
, LF
, FF
, BELL
...) depending on the character set in use.
For ASCII
char c = 'A';
is the same as
char c = 65;
is the same as
char c = 0x41;
Another character set widely in use for example is EBCDIC. It uses a different mapping of a character's integer value to its printable/controling representation.
Internally always the same integer value is used/stored.
The printable, often but not always ASCII representation of, for example 65
or 0x41
, which is A
, is only used when
printf()
-family along with the conversion specifiers %s
or %c
or puts()
scanf()
-family along with the conversion specifiers %s
or %c
or fgets()
'A'
or "ABC"
.On all other operation only the char
's integer value is used.
When you do calculations with char
s, you have to keep in mind that to you it looks like a '0' or '9', but the compiler interprets is as its ASCII value, which is 48 for '0' and 57 for '9'.
So when you do:
int x=9-c1;
the result is 9 - 48 = -39. And for
int y='9'-c1;
the result is 57 - 48 = 9.
According to the C Standard (5.2.1 Character sets)
- ...In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
Thus expression '9' - '0'
has the same value like 9 - 0
and is equal to 9 whether you are using for example the ASCII table of characters or the EBCDIC.
Expression 9 - '0'
is implementation defined and depends on the coding table you are using. But in any case the value of the internal representation of character '0'
is greater then 9
. (9
is the value of the tab character representation '\t'
)
For example in the ASCII the value of the code of character '0'
is equal to 48
.
In the EBCDIC the value of '0'
is equal to 240
.
So you will get that 9 - '0'
is some negative number.
For example it is equal to -39
if the character representations are based on the ASCII table or -231
if the character representations are based on the EBCDIC table.
You can see this yourself running this simple program
#include <stdio.h>
int main( void )
{
printf( "%d\n", 9 - '0' );
}
You could write the printf statement also in the following way;)
printf( "%d\n", '\t' - '0' );
because 9
as I mentioned is the value of the internal representation of the escape character '\t'
(tab).