Home > database >  Why doesn't putchar(1 '0') output 10?
Why doesn't putchar(1 '0') output 10?

Time:12-13

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.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

  • Related