Are signed, unsigned, long and short all valid types in all versions of C and C++?

StackOverflow https://stackoverflow.com/questions/22493879

  •  17-06-2023
  •  | 
  •  

문제

In N3797 7.1.1/3 there is the following note:

Note: Since signed, unsigned, long, and short by default imply int, a type-name appearing after one of those specifiers is treated as the name being (re)declared.

But this is only a note. Is there somewhere in the standard that makes a normative statement to the same effect?

Is is proper standards-compliant C and C++ to use signed, unsigned, long and short without int in each and every situation?

Or, to put it in code, are the following standards-compliant and if so where does it say so?

signed a;
unsigned b;
short c;
long d;
signed f(signed p, unsigned q, short r, long s);
unsigned* f(signed* p, unsigned* q, short* r, long* s);

[I hesitate to ask whether it's recommended or preferred, for fear of getting opinion-based answers.]

도움이 되었습니까?

해결책

As far I know it has always been that way but we can at least go back to the earliest publicly available draft standards. For C++ would be 1804 and in section 7.1.5.2 Simple type specifiers Table 7 simple-type-specifiers and the types they specify includes the following entries:

unsigned   “unsigned int”
signed     “int”
long       “long int”
short      “short int”

for C we can go back to C99 which in section 6.7.2 Type specifiers says:

[...]Each list of type specifiers shall be one of the following sets (delimited by commas, when there is more than one set on a line); [...]

and includes the following bullets:

— short, signed short, short int, or signed short int

— int, signed, or signed int

— unsigned, or unsigned int

— long, signed long, long int, or signed long int

So these all all standards compliant type specifiers and both quotes come from the normative section of the respective draft standards.

다른 팁

With the exception of char (signed char and char are different types), yes, to answer if it's always true, we can go back to the oldest standard: C89 and C++98.

C89 §3.5.2 Type specifiers

Each list of type specifiers shall be one of the following sets; the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

  • void

  • char

  • signed char

  • unsigned char

  • short , signed short , short int , or signed short int

  • unsigned short , or unsigned short int

  • int , signed , signed int , or no type specifiers

  • unsigned , or unsigned int

  • long , signed long , long int , or signed long int

  • unsigned long , or unsigned long int

  • float

  • double

  • long double

  • struct-or-union specifier

  • enum-specifier

  • typedef-name

Semantics

Specifiers for structures, unions, and enumerations are discussed in 3.5.2.1 through 3.5.2.3 Declarations of typedef names are discussed in 3.5.6 The characteristics of the other types are discussed in 3.1.2.5

Each of the above comma-separated lists designates the same type, except that for bit-field declarations, signed int (or signed ) may differ from int (or no type specifiers).

Similarly in C++98 §7.1.5.2 Simple type specifiers:

C++98 type specifiers

Regarding your question: Is i[t] proper standards-compliant C and C++ to use signed, unsigned, long and short without int in each and every situation?

Because there is also such a thing as unsigned char, the answer is No, not in every situation. i.e. if you need an unsigned char, you need to specify char. The idea of the note you refer to is to say that if the explicit type is not used, then by default the type will be int.

For a more exact answer you can read section 6.2.5 of the c11 standard. Paragraphs 4 & 6 are reproduced here for their relevance, but the other sections will also shed light on other parts of your question:

4

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types.38) The standard and extended signed integer types are collectively called signed integer types.39)

6

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. The type Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types. The unsigned integer types that correspond to the extended signed integer types are the extended unsigned integer types. The standard and extended unsigned integer types are collectively called unsigned integer types.40)

An online C++ reference can be found HERE
Specifically look at the properties table.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top