Reputation: 5534
I am trying to understand how the putchar('0' + r);
works. Below, the function takes an integer and transform it to binary.
void to_binary(unsigned long n)
{
int r;
r = n % 2;
if (n >= 2)
to_binary(n / 2);
putchar('0' + r);
}
I google the definition of putchar but I didn't find this. To test it, I added a printf to see the value of the r:
void to_binary(unsigned long n)
{
int r;
r = n % 2;
if (n >= 2)
to_binary(n / 2);
printf("r = %d and putchar printed ", r);
putchar('0' + r);
printf("\n");
}
and I run it (typed 5) and got this output:
r = 1 and putchar printed 1
r = 0 and putchar printed 0
r = 1 and putchar printed 1
So I suppose that the putchar('0' + r);
prints 0 if r=0, else prints 1 if r=1, or something else happens?
Upvotes: 8
Views: 8801
Reputation: 1420
U are adding the ASCII value of the number's say '0' ASCII value is 48
'1' -> 49,and so on CHECK HERE FOR COMPLETE TABLE
so when u add one to 48 it will 49 and putchar functuion prints the character sent to it. when u do
putchar('0' + r )
if r = 1 putchar(48 + 1) (converting into ASCII value)
putchar(49) which is 1
Upvotes: 0
Reputation: 50667
It's a common technique used for char
handing.
char a = '0' + r
(r in [0,9]) will convert an integer to its char format based on given char base (i.e. '0'
in this case), you will get '0'...'9'
Similarly, char a = 'a' + r
or char a = 'A' + r
(r in [0,25]) will convert an integer to its char format, you will get 'a'...'z'
or 'A'...'Z'
(except for EBCDIC systems which has discontinuous area for alphabets).
Edit:
You can also do the other way around, for example:
char myChar = 'c';
int b = myChar - 'a'; // b will be 2
Similar idea is used to convert a lowercase char to uppercase:
char myChar = 'c';
char newChar = myChar - 'a' + 'A'; // newChar will be 'C'
Upvotes: 2
Reputation: 182649
In C '0' + digit
is a cheap way of converting a single-digit integer into its character representation, like ASCII or EBCDIC. For example if you use ASCII
think of it as adding 0x30 ('0'
) to a digit.
The one assumption is that the character encoding has a contiguous area for digits - which holds for both ASCII and EBCDIC.
As pointed out in the comments this property is required by both the C++ and C standards. The C standard says:
5.2.1 - 3
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.
Upvotes: 6
Reputation: 9930
'0'
represents an integer equal to 48 in decimal and is the ASCII code for the character 0
(zero). The ASCII code for the character for 1
is 49 in decimal.
'0' + r
is the same as 48 + r
. When r = 0, the expression evaluates to 48 so a 0
is outputted. On the other hand, when r = 1, the expression evaluates to 49 so a 1
is outputted. In other words, '0' + 1 == '1'
Basically, it's a nice way to convert decimal digits to their ASCII character representations easily. It also works with the alphabet (i.e. 'A' + 2
is the same as C
)
Upvotes: 4