0

Can someone explain me why the index of cvorkommen (zeichen_c - '0' works as an integer 4?

#include <stdio.h>
#include <string.h>

int main (void) {
  char cvorkommen [100] = {0};
  char zeichen_c = '4';

  cvorkommen[zeichen_c - '0']++
 
  return 0;
}
5
  • 2
    all arithmetic operations yield an int (or unsigned) or larger type... so char - int (your code -- [try sizeof '0' to see for yourself that '0' is not a char]) gives an int... char - char gives an int... char + short gives an int... char * char gives an int ... Commented Jun 21, 2022 at 7:26
  • char are basically integers, and to each integer is attributed a character Commented Jun 21, 2022 at 7:27
  • Related: stackoverflow.com/q/46890093/6699433 Commented Jun 21, 2022 at 8:47
  • @klutt Arithmetic on digits '0' to '9' specifically isn't implementation-defined. They are a special case. Commented Jun 21, 2022 at 10:06
  • @Lundin I know. That's why I didn't dupe it. It's releated. Commented Jun 21, 2022 at 10:37

2 Answers 2

3

For the 10 numeric digit characters the C standard requires them to be in sequence:

5.2.1 Character sets

3 ... In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

Other characters, such as alphabet letters, are not required to be in sequence. The uppercase and lowercase ASCII character alphabet characters are in sequence, but the EBCDIC ones are not.

So to find the numeric value of a digit character, it is always safe to subtract the base case

char digit = '4';
int number = digit - '0';
Sign up to request clarification or add additional context in comments.

Comments

3

That works because you take the char zeichen_c which has the ASCII value of '4' (0x34) and subtract the ASCII value of '0' (0x30) from it. That leaves you with 0x4 which is a valid index of your array.

3 Comments

This isn't exclusive to ASCII. It also works on an EBCDIC machine since digits are continuous and ordered there too. It's just that it would be 0xF4 - 0xF0 = 4 there.
The answer is correct. I rather would like to add that char is basically just a 1 byte (8-bit) integer. It would have normal integer values (but the limit of the values is set, which is -128 to 127). All ASCII characters are simple integers. You could even see their integer values by using printf with format %d. This way, you could use char as a counter, just the way you would have it with int (but be sure that your counter always counts less than or equal to 127, or else you will end up doing overflow).
@user1234: limits for char are not set. In a lot of personal computers limits will be -128 to 127, but can also be 0 to 255. In ALL computers limits are CHAR_MIN to CHAR_MAX :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.