1

When writing '9' instead of 9 as my char c my endresult becomes negative and wrong for some reason. Why is that? What does '9' do?

#include <stdio.h>

int main() {
    int a, b;
    char c = '9';
    a = 44;
    b = a - c;

    printf("%d \n", b);

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Cryme
  • 21
  • 3
  • 1
    `'9'` means the ASCII glyph/character that is 9. `9` means the number 9. ASCII characters are integers, so `'9'` is really the integer value 57. – heptadecagram Mar 28 '23 at 15:05
  • 2
    `9` is an integer, a number, one less than the number of fingers on your hand. `'9'` is a character, ASCII code 57, one less than `':'`. (Try printfing them both using `%d`.) – Steve Summit Mar 28 '23 at 15:06
  • possible duplicates: [usage of two characters in single quotes in c](https://stackoverflow.com/q/12212668/995714), [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/q/3683602/995714). You need to [read a good C book](https://stackoverflow.com/q/562303/995714), stackoverflow isn't a place for asking things like this – phuclv Mar 28 '23 at 15:10
  • If you want to subtract nine from something, you definitely want to subtract `9`. Subtracting `'9'` is pretty meaningless, since it's analogous to subtracting `'Q'`, or subtracting `'$'`. – Steve Summit Mar 28 '23 at 15:10
  • 1
    @SteveSummit that's quite a hand! – mnistic Mar 28 '23 at 15:10
  • @mnistic I need to ask you to add an `'s'` (not 115) to the word "hand" in that comment... – Steve Summit Mar 28 '23 at 15:19

3 Answers3

5

'9' and 9 are both integer constants.

  • 9 has integer value of nine
  • '9' - has integer value of the (usually) ASCII code of symbol '9'. It is 57 It is for our convenience when we deal with strings and characters.
    char c = '9';
    int a = 44;
    int b = a - c;
  • char is also integer type having size 1 (sizeof(char) is by definition 1) Variable c has integer value of 57.
  • c is being converted to int and it is subtracted from a (44-57). The result is assigned to b

ASCII table: enter image description here

There are other coding conventions and standards (like EBCDIC), but it is very unlikely nowadays to work on the machine which uses them

0___________
  • 60,014
  • 4
  • 34
  • 74
  • @ikegami it is not correct of course as compiler needs to convert to the correct single character integer. In source code it can be even more than one byte. Examples: `'\n'` `'\141'` `'\x33'` – 0___________ Mar 28 '23 at 16:03
  • @ikegami, it is actually the value of the character `9` in the execution character set. That's only incidentally connected to the value in the source file, via the fact that source and execution character sets are usually the same. But they don't *have to be* the same. – John Bollinger Mar 28 '23 at 16:09
  • It's not necessarily ASCII. It could be another character set. Like EBCDIC. – klutt Mar 28 '23 at 16:11
  • @0___________, I was obviously talking about unescaped characters, just like you were. – ikegami Mar 28 '23 at 16:38
  • @klutt did you ever program `EBCDIC` computer in C language? – 0___________ Mar 28 '23 at 16:39
  • @John Bollinger, It did seem wrong after I wrote it. Deleted. – ikegami Mar 28 '23 at 16:39
  • 0___________, I've submitted patches to `perl`, so technically yes I have. – ikegami Mar 28 '23 at 16:40
  • @ikegami did you use IBM mainframe for that? Or your PC? I am asking if someone here was programming the machine using EBCDIC in C – 0___________ Mar 28 '23 at 16:47
  • I did not run the code directly -- that's what automated testing is for -- but the code needed to run on such machines, yes. Perl supports EBCDIC machines, and it's written in C. /// I also deal with data that's encoded using EBCDIC on a daily basis, but the systems I use are ASCII-based. – ikegami Mar 28 '23 at 16:57
  • @0___________ There are tons of things that I have not done. That does not make them impossible. – klutt Mar 28 '23 at 20:19
1

'9' is a representation of 57, which is the index at which the glyph 9 shows up in the ASCII table. As such, a-c equals to 44-57, which yields the negative result.

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

char a = '9' means a's value is the ascii code of the character 9, , which is 57 instead of the value 9.

So b = a - c is actually 44 - 57.