Who determines the ordering of characters

前端 未结 6 1889
轻奢々
轻奢々 2020-12-19 03:17

I have a query based on the below program -

char ch;
ch = \'z\';
while(ch >= \'a\')
{
    printf(\"char is  %c and the value is %d\\n\", ch, ch);
    ch =         


        
6条回答
  •  时光说笑
    2020-12-19 04:02

    The compiler implementor chooses their underlying character set. About the only thing the standard has to say is that a certain minimal number of characters must be available and that the numeric characters are contiguous.

    The required characters for a C99 execution environment are A through Z, a through z, 0 through 9 (which must be together and in order), any of !"#%&'()*+,-./:;<=>?[\]^_{|}~, space, horizontal tab, vertical tab, form-feed, alert, backspace, carriage return and new line. This remains unchanged in the current draft of C1x, the next iteration of that standard.

    Everything else depends on the implementation.

    For example, code like:

    int isUpperAlpha(char c) {
        return (c >= 'A') && (c <= 'Z');
    }
    

    will break on the mainframe which uses EBCDIC, dividing the upper case characters into two regions.

    Truly portable code will take that into account. All other code should document its dependencies.

    A more portable implementation of your example would be something along the lines of:

    static char chrs[] = "zyxwvutsrqponmlkjihgfedcba";
    char *pCh = chrs;
    while (*pCh != 0) {
        printf ("char is %c and the value is %d\n", *pCh, *pCh);
        pCh++;
    }
    

    If you want a real portable solution, you should probably use islower() since code that checks only the Latin characters won't be portable to (for example) Greek using Unicode for its underlying character set.

提交回复
热议问题