Is 'long unsigned' as valid as 'unsigned long' in C?

前端 未结 2 1074
别那么骄傲
别那么骄傲 2020-12-03 22:14

A question was recently asked about whether ULL or LLU was valid for specifying unsigned long long constants in C. I know they\'re both valid but I

相关标签:
2条回答
  • 2020-12-03 22:53

    The ISO C11 standard states in 6.2.5 Types:

    There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int.

    For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements.

    However there's no mandate in that section as to the order in which the base type and unsigned modifier appears.

    The controlling section is later in the standard, 6.7.2 Type specifiers, paraphrased here:

    Type specifiers are void, char, short, int, long, float, double, signed, unsigned, _Bool, _Complex, <struct-or-union-specifier>, <enum-specifier>, and <typedef-name>.

    At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

    It then goes on to list all the multisets, such as unsigned long, or unsigned long int.

    But the important phrase there is the type specifiers may occur in any order, meaning that all of these are valid for that multiset:

    unsigned long
    long unsigned
    
    unsigned long int
    unsigned int long
    long unsigned int
    long int unsigned
    int unsigned long
    int long unsigned
    
    0 讨论(0)
  • 2020-12-03 23:08

    The order of the specifier dosen't matter.

    unsigned long long  is the same as long long unsigned.  
    

    Both types are valid according to C standard(c99).

    0 讨论(0)
提交回复
热议问题