### putchar('0' + num)是什么?做什么?

#### [英]what does putchar('0' + num); do?

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

r = 1, putchar打印1 r = 0, putchar打印0 r = 1, putchar打印1。

So I suppose that the `putchar('0' + r);` prints 0 if r=0, else prints 1 if r=1, or something else happens?

## 4 个解决方案

### #1

6

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

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.

### #2

3

`'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'表示一個整數，等於48的小數，是字符0(0)的ASCII碼。字符1的ASCII碼為十進制的49。

`'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'`

“0”+ r與48 + r相同，當r = 0時，表達式為48，因此輸出為0。另一方面，當r = 1時，表達式的計算值為49，所以1被輸出。換句話說，'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`)

### #3

2

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'`

char a = '0' + r (r in[0,9])將根據給定的char基數將整數轉換為其char格式(即:在這種情況下，你將得到'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:

1. You can also do the other way around, for example:

你也可以反過來，例如:

``````char myChar = 'c';
int b = myChar - 'a'; // b will be 2
``````
2. Similar idea is used to convert a lowercase char to uppercase:

類似的想法用於將小寫字符轉換為大寫:

``````char myChar = 'c';
char newChar = myChar - 'a' + 'A'; // newChar will be 'C'
``````

### #4

0

U are adding the ASCII value of the number's say '0' ASCII value is 48

U正在添加數字的ASCII值，即“0”ASCII值為48。

'1' -> 49,and so on CHECK HERE FOR COMPLETE TABLE

“1”-> 49，因此在這里查看完整的表格。

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

putchar(49)是1 #### 注意！

© 2014-2021 ITdaan.com