Home > Software engineering >  Comparing single quote numbers instead of regular numbers(numbers without quotes)? C Programming Lan
Comparing single quote numbers instead of regular numbers(numbers without quotes)? C Programming Lan

Time:10-21

This is part of a code to count white spaces, numbers, or other from the K&R "C programming book." I am confused why it compares "int c" to digits using '0' and '9' instead of 0 and 9. I realize the code doesn't work if I use 0 and 9 without quotes. I am just trying to understand why. Does this have to do with c being equal to getchar()?

while ((c = getchar()) != EOF)
    if (c >= '0' && c <= '9')
          ndigit[c-'0'];
    else if (c == ' ' || c == '\n' || c == '\t')
          nwhite;
    else
          nother;

CodePudding user response:

looking at the man page for getchar, we see that it returns the character read as an unsigned char cast to an int. So we can assume the value stored is not an integer number, but its ascii equivalent, and can be compared with chars such as '0' and '9'.

CodePudding user response:

A char usually is just an integer. Where the meaing is given by some charset. For example ASCII.

So for example we could store "Hello" as the sequence 72, 65, 108, 108 and 111.

Using single quotes (as in '9') we tell that we mean the number which represents the character '9'. Behind the scenes the computer only knows numbers and so this will end up in the code 57 for our example (see char '9', in red, maps to code 57 in the ASCII table). For more examples see linked ASCII table above.

Same counts for the chars in our input data. Also those are encoded into those numbers according to the charset we're using.

In contrast if we would just use a plain 9 we would ask for exactly the code 9. And not "the code which represents char 9". That's the difference.

BTW: There's another "trick" used in the code sample. it is c-'0' which asks to subtract "the code behind the character '0'" from our current character c. If we do this, we will end up with the digit not as the character, but as the number behind it. Example:

  • Assume c is the character '4'.
  • So in c it is stored as the code 52 (see ASCII table)
  • If we now want the numeric value 4 in place of the character '4' we just subtract the character '0' from it (code 48 in ASCII)
  • So 52 - 48 will end up as 4 (not a char but the number behind it)

CodePudding user response:

getchar() returns a signed integer in order to allow it to return EOF (-1). If it returned a char, you could not have an error value.

Moreover '9' is a literal character constant, whose value is the character set code for the digit character '9' and not the integer value 9, and in C (but not C ) has type int, so there is in any case no type mismatch in the expression c <= '9' for example, it is an int comparison.

Even if that were not the case, and a literal character constants had char type (as in C ), there would be an implicit type promotion to int before comparison.

Also, you need to understand that a char is not specifically a character, but rather simply an integer type that is the:

Smallest addressable unit of the machine that can contain basic character set.

  •  Tags:  
  • c
  • Related