FrozenHeart
FrozenHeart

Reputation: 20746

Order of the characters in character set

Is it guarantees by the standard the order of the characters? Can i count that '1' symbol are followed in the character set table by the '2' symbol, for example? Or is it platform-specific?

Upvotes: 5

Views: 1633

Answers (2)

WhozCraig
WhozCraig

Reputation: 66194

The standard calls for numeric chars to be sequential, and indeed for all platforms I'm aware of '0' through '9' are, in fact, ordered and sequential. The same can NOT be said about alphabets in general. I point you to any of the EBCDIC platforms (AS/400, OS390, etc) for samples where this is most definitely not the case.

I.e. you can reliable do this:

for (char ch = '0'; ch <= '9'; ch++)

but you can NOT reliable do this:

for (char ch = 'a'; ch <= 'z'; ch++)

and expect the latter to sling through 26 iterations. It will be platform dependent.

Note: It is NOT specified in the standard that order outside of the digits (which are both ordered and sequential) is guaranteed. Still, all platforms i've worked on (and that is a big number) exhibit consistent order properties even to alphabets, but only similar case. I.e. 'a' is always "less than" 'z'. But just like a bad penny, then comes back in EBCDIC platforms. For standard platforms 'A' is always less than 'z', but not in EBCDIC.

Bottom line: except for digit chars, you can't reliably assume near-anything about order or sequence and still maintain pure platform independence.

Upvotes: 5

Alexey Frunze
Alexey Frunze

Reputation: 62048

The C standard from 1999 says this about the character sets:

Both the basic source and basic execution character sets shall have the following members:
the 26 uppercase letters of the Latin alphabet
...
the 26 lowercase letters of the Latin alphabet
...
the 10 decimal digits
0 1 2 3 4 5 6 7 8 9
the following 29 graphic characters
...
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.

Upvotes: 7

Related Questions