2

Why does putchar outputs '1' for putchar(1+'0') but not '10' but when only a character argument is passed, like putchar('0'), it outputs it.

With putchar(1+'0'), I expected an output of 10.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Because operators in C behaves as they are specified. They do not necessarily behave like operators in some other random programming language or as you think they should behave. You could however do `puts("1" "0");` to get the behavior you describe. – Lundin Dec 12 '22 at 14:22
  • `1 + '0' == '1'`. It's the character whose value is one more than the character value of `'0'`. It's just integer addition. – Tom Karzes Dec 12 '22 at 15:01

5 Answers5

7

putchar always outputs a single character only, as per the name.

'0' is a single character literal. So it’s an integer value that represents the character 0 in your computer’s encoding — almost certainly ASCII.

1+'0' then literally means “the character that comes one after 0 in my computer’s encoding (which is almost certainly ASCII). Which is the character 1.

Clifford
  • 88,407
  • 13
  • 85
  • 165
Tommy
  • 99,986
  • 12
  • 185
  • 204
  • and `putchar(4)` outputs the integer 4. can you explain it for me? – ElormCodes1 Dec 12 '22 at 12:52
  • 1
    @ElormCodes1 In a typical, ASCII -compatible character set, 4 is the value of the [␄ character](https://en.wikipedia.org/wiki/End-of-Transmission_character). – Shawn Dec 12 '22 at 13:06
  • @ElormCodes1 refer to this [ASCII-table](https://ascii-tables.com/) – theemee Dec 12 '22 at 13:08
  • 3
    @ElormCodes1: Re “and `putchar(4)` outputs the integer 4”: No, it does not, if by “integer 4” you actually mean the character “4”. Did you try it? – Eric Postpischil Dec 12 '22 at 13:30
4

Unlike languages such as Python, the + operator in C is not used to concatenate strings.

What you actually have here with 1+'0' is that you're adding the value 1 with the character code for the character 0. The C standard guarantees that the characters for the numerals 0 to 9 have consecutive character encodings, so adding 1 to the character code for 0 gives you the character code for 1, which is what gets printed.

dbush
  • 205,898
  • 23
  • 218
  • 273
3

Understand that in C, there aren't really chars. They are just integers.

putchar(48); prints "0"
putchar(49); prints "1"
putchar(65); prints "A"

Because 48 is the ascii reprentation of character 0, 49 of 1, 65 of A. That is not even C decision. That is the terminal job. putchar just output the integer it was given as an argument to the output terminal, and it does the job of drawing a nice oval if pushed 48, a nice vertical bar if pushed 49, or a upside down V with a transversal bar if pushed 65 (I described it voluntarily this trivial way to make clear how it is just a drawing convention).

Now, the second thing to understand is that in C, for the same reason (there aren't really chars), there are several ways to write a literal integer. 48 for example can be written
in decimal, 48
in hexadecimal, 0x30
in octal, 060
and in "char" representation, '0'.

'0' is just a way to type 48.

The only reason why you wouldn't type this code
printf("Di Caprio is %d years old\n", '0');
is not because it wouldn't work. It is exactly the same as if you had typed
printf("Di Caprio is %d years old\n", 48);

The reason why you don't do that, is because it is an awful style. A human reader of your code would have hard time to understand it. Normally you don't use '0' to say 48, unless you mean "48 as the ascii code of 0"
(Plus, theoretically, you are not supposed to know that 48 is the ascii code of 0. The whole point of using that notation is to let that decision to the compiler).

So, back to your example, when you said
putchar(1+'0');
it is exactly the same code as if you had
putchar(1+0x30);
or
putchar(1+060);
or
putchar(1+48);
so, in the end
putchar(49);
and, since 49 is the ascii code of 1
putchar('1');

chrslg
  • 9,023
  • 5
  • 17
  • 31
1

'0' is a literal character constant for the digit character 0. In ASCII/ANSI character sets it has a numeric value 0x30 (or 48 decimal). If you add 1 to it you get a value 0x31 - the character code for '2', and putchar() interprets the parameter as a single character code and presents the appropriate character to stdout.

If you wanted to output "10" you would need:

putchar('1') ;
putchar('0') ;
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Clifford
  • 88,407
  • 13
  • 85
  • 165
0

In C a character in single quotes is a char. But a char in C is also just a number that encodes some ASCII character.

The character '0' corresponds corresponds to number 48. So 48 is it's ASCII code.

So when you add an int 1 with a char '0', the char '0' is converted to an int. So basically 1 + 48, which is 49.

Then you try to call putchar(49) which converts the 49 to it's ASCII character which is '1'.

theemee
  • 769
  • 2
  • 10
  • 1
    The `char` type is used to hold character codes (among other things), but it is not always ASCII. ASCII is very common, but not all C implementations use ASCII, and the C standard does not require it. – Eric Postpischil Dec 12 '22 at 13:33