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.

我試着理解putchar('0' + r)是怎樣的;的工作原理。下面,函數接受一個整數並將其轉換為二進制。

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:

我谷歌定義了putchar,但我沒有找到這個。為了測試它,我添加了一個printf來查看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:

我運行它(鍵入5)並得到這個輸出:

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?

所以我想,putchar('0' + r);如果r=0,則輸出0,如果r=1,則輸出1,否則會發生什么?

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.

在C '0' +數字中,是一種將單個數字的整數轉換成其字符表示形式的廉價方法,如ASCII或EBCDIC。例如,如果使用ASCII,則將其視為將0x30('0')添加到一個數字。

The one assumption is that the character encoding has a contiguous area for digits - which holds for both ASCII and EBCDIC.

一種假設是字符編碼有一個連續的數字區域,它對ASCII和EBCDIC都適用。


As pointed out in the comments this property is required by both the C++ and C standards. The C standard says:

正如在評論中指出的,這個屬性是c++和C標准所要求的。C標准說:

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)

基本上,這是一種很好地將十進制數字轉換為ASCII字符表示的方法。它也適用於字母表。A + 2和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).

類似地,char a = 'a' + r或char a = 'a' + r (r in[0,25])將把整數轉換成char格式,您將得到'a'……“z”或“”……“Z”(除了EBCDIC系統,該系統在字母表上有不連續的區域)。


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

所以當你添加1到48的時候,它將會打印出發送給它的字符,49和putchar功能。當你做

putchar('0' + r ) 

if r = 1 putchar(48 + 1) (converting into ASCII value)

如果r = 1 putchar(48 + 1)(轉換為ASCII值)

putchar(49) which is 1

putchar(49)是1


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/01/21/720d81b6022db0ef5f6efcb8e14f4fc9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com