Are the ASCII values ​​'a' through 'z' sequentially from 97 to 122 regardless of implementation? A good book says differently

I don't see anything wrong with the following program and this non-portable is really confusing me. According to Mike Banahan's book (GBdirect C Book, section 2.4.2) , the following program is not portable. Reason given:

Perhaps another example. This will either print out the entire lowercase alphabet if your implementation has its characters stored sequentially, or something even more interesting if they aren't. C doesn't make many guarantees about the ordering of characters internally, so this program produces non-portable results!

So, in simple terms, can you explain to me what happened to the program below? Are the ASCII character values ​​the same regardless of implementation? I mean, the "a" value is always 97 and the "b" value is always 98; so why get the latter by adding 1 not portable?

#include <stdio.h>
#include <stdlib.h>
main(){
      char c;

      c = 'a';
      while(c <= 'z'){
              printf("value %d char %c\n", c, c);
              c = c+1;
      }

      exit(EXIT_SUCCESS);
}

      

+3


source to share


1 answer


C does not require ASCII encoding. It accepts other encodings, some of which may not have letters represented by consecutive values.

An example is EBCDIC , where the letters are not sequential.



Note that the characters for numbers are guaranteed by the C standard to always be in a row (although they may not have the values ​​48-57 as in ASCII).

+4


source







All Articles