what does putchar('0' + num); do?
Asked Answered
T

4

8

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?

Twi answered 21/1, 2014 at 10:10 Comment(0)
S
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

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.

Settlement answered 21/1, 2014 at 10:12 Comment(3)
"The one assumption .." is actually required: #9417426Vinery
@Vinery Thanks. I added the quote for the C standard - I didn't know if C++ also had this.Settlement
However keep in mind that if you're writing a file or network stream parser that has to work from a specific character set you should code it against the numeric values found in that character set, just to be clear about it (also to somebody reading the code).Hammerhead
K
4

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

Krys answered 21/1, 2014 at 10:14 Comment(2)
The part about 'A' + n is not always true with EBCDIC systems... See cnicutar's answer.Brackish
Indeed, the standard doesn't enforce 'A'+n, not even with a lower limit (such as making it mandatory for the first six letters). It happens to work in ASCII for all unaccented latin letters and in EBCDIC for A to I (enabling easy hexadecimal conversion), but a charset could exist without such trait.Melba
B
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'

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' 
    
Benil answered 21/1, 2014 at 10:16 Comment(1)
The part about 'A' + n and the character codes for the alphabet in general is not always true with EBCDIC systems... See cnicutar's answer.Brackish
M
0

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

Manger answered 21/1, 2014 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.