What does putchar ('0' + num) do; make?

I am trying to understand how putchar('0' + r); works putchar('0' + r); . The function below takes an integer and converts 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 have google putchar definition but I have not found this. To test this, I added printf to see the r value:

 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 ran 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 putchar('0' + r); prints 0 if r = 0, else prints 1 if r = 1, or is something else going on?

+6
source share
4 answers

In C '0' + digit is a cheap way to convert a single-valued integer to its character representation, such as ASCII or EBCDIC. For example, if you use ASCII , think about this by adding 0x30 ( '0' ) to the digit.

One of the assumptions is that the character encoding has an adjacent area for digits, which is performed for both ASCII and EBCDIC.


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

5.2.1 - 3

In both basic sets of source and execution, the value of each character after 0 in the above list of decimal digits must be equal to one greater than the value of the previous one.

+6
source

'0' represents an integer equal to 48 in decimal value, and is the ASCII code for the character 0 (zero). The ASCII code for the character for 1 is 49 in decimal.

'0' + r matches 48 + r . When r = 0, the expression evaluates to 48, so 0 is output. On the other hand, when r = 1, the expression evaluates to 49, so 1 is output. In other words, '0' + 1 == '1'

Basically, this is a good way to easily convert decimal digits to their ASCII character representation. It also works with the alphabet (i.e. 'A' + 2 matches C )

+3
source

This is a common method used to pass char .

char a = '0' + r (r in [0,9]) converts an integer to char format based on the given char base (ie '0' in this case), you will get '0'...'9'

Similarly, char a = 'a' + r or char a = 'a' + r (r in [0,25]) converts an integer to its char format, you get 'a'...'z' or 'a'...'z' (except for EBCDIC systems , which has a discontinuous area for alphabets).


Edit:

  • You can also do another way, for example:

     char myChar = 'c'; int b = myChar - 'a'; // b will be 2 
  • A similar idea is used to convert the lowercase char to uppercase:

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

U adds the ASCII value of the number say '0' The ASCII value is 48

'1' β†’ 49 etc. CHECK HERE FOR THE FULL TABLE

therefore, when u add one of 48, it will be 49, and the putchar function will print the character sent to it. when u do

 putchar('0' + r ) 

if r = 1 putchar (48 + 1) (conversion to ASCII value)

putchar (49) which is 1

0
source

All Articles