Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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

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:

  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' 
    

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top