About putchar in C programming

I saw this code

int main() {

          int r = 'H';
          putchar( r + '0');

         return 0;
} 

I’m asking why ‘0’ is added to r
Why must my ‘0’ be added?

This is not great code. I am trying to figure out what they are doing, but the correct version of that code would be

#include <stdio.h>

int main() {
  char r = 'H';
  putchar(r);
  return 0;
} 

I think that they are converting the int into a char with the + '0', but that’s a really annoying way to do it. It works by converting to the character code for the digit. And I don’t think it will produce sensible output in this case - the H will be offset by ‘0’, so you’ll print a completely different character.


#include <stdio.h>
#include <unistd.h>

int _islower(int c);
int _putchar(char c);


int main() {

        int r;
        r = _islower(72);
    _putchar(r);
        return 0;
}

int _islower(int c)
{
        return (c >= 'a' && c <= 'z');
}


int _putchar(char c)
{
        return (write(1, &c, 1));
}

This is the full code

Either ways, what I really want to know is why add ‘0’ at all
What are the implications of adding it?

Writing function names like this is a big no-no! Wherever you are learning from is teaching you bad things!

#include <stdio.h>
#include <unistd.h>

int islower_handrolled(char c);
int putchar_handrolled(char c);

int main() {
  char letter = 'H';
  int isletterlower = islower_handrolled(letter);

  putchar_handrolled(letter);

  return 0;
}

int islower_handrolled(char c) {
  return c >= 'a' && c <= 'z';
}

int putchar_handrolled(char c) {
  return write(1,  &c, 1);
}

There is a lot going on here.

  • Don’t prefix function names with _. The underscore prefix is reserved for use by compilers and standard libraries.

  • You are mixing two types here - int and char. An int is just a number, but a char human readable letter. Internally, C stores every char as a number, and you can see the conversion table here: ASCII - Wikipedia

  • The '0', like I said above, is converting between an int to its representation as a char. You can perhaps see it more clearly as a function:

#include <stdio.h>

char inttochar(int i) {
  return i + '0';
}

int main() {
  int num = 5;

  printf("this is a number printed as a number: %d\n",  num);
  printf("this is a number printed as a char: %c\n", num);
  printf("this is a number converted to a char: %c\n", inttochar(num));

  return 0;
}

You can see the output of this short program here

The ASCII character code for '0' is 48, and the ASCII character code for '5' is 53. You can convert the int of 5 into the char of '5' by adding 48, or '5' == '0' + 5.

1 Like

Wow!
Thank you for this detailed explanation

From the above codes you’ve written what would ’H* + ‘0’ output

You can test it and see. I would try it so you know for sure.

char + char doesn’t make a ton of sense, but the computer would add the two ASCII character codes corresponding to the two letters to get a new character code and then display the corresponding character.

1 Like