Can an unsigned keyword be used in a non-obvious way?

Each time I used the unsigned keyword, it came before an int or other built-in type. I was wondering if other unsigned methods can be used.

  • Can custom types (classes / structures) use the unsigned keyword?
  • Can templates be used with unsigned ?

If not, why does he have his own keyword? Why is unsigned int not uint ?

+6
source share
6 answers

No, it cannot be used with classes or structures, since they are not integer types. All the template can do is int unsigned. I think this was chosen as a separate keyword, since it can be applied to any integer type (char, int, long, long long), thereby reaching one keyword, which would require four more. Its meaning also immediately manifests itself, while uint, uchar, etc. Not necessary. And keep in mind that it can also be used on its own without a qualifier, in which case it is assumed that unsigned int is used.

+4
source

The answer to the main question was asked several times: the unsigned keyword can only be used as a type specifier for an integral type.

As to why unsigned is a separate keyword and not a uint keyword, the reasons for this are historical.

The earliest versions of C (pre-K & R) had only four main types:

  • char (8 bits, signed, 2'-complement)
  • int (16 bits, signed, 2'-pad)
  • float (32 bit)
  • double (64 bits, same range as float , but greater accuracy)

Note that it is missing: no signed or unsigned keywords, no short , long or long double ; they were all added later. (Programmers who need unsigned arithmetic commonly used pointers that were freely interchangeable with int .)

Each fundamental type had a name, which was one keyword, which made grammar simple.

When other types were added later, it made sense to add qualifiers such as unsigned , short and long to existing type names rather than entering new keywords (which could break existing code). When the ANSI C committee standardized this language in 1989, they had to make a coherent structure from existing not quite formal definitions, while remaining in line with existing implementations. The result is what we have now, where int long unsigned long is a valid type name (most often written as unsigned long long ).

If the language was developed from scratch, I suspect that a different approach would be used. Perhaps for each fundamental type there will be one keyword (for example, the C # approach) or, perhaps, fundamental type names will use a more consistent scheme, rather than a mess of keywords (for example, int:2 for 2 -byte integer, unsigned:4 for a 4-byte unsigned integer). But C and C ++ are stuck in the current approach.

Link: http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf

+5
source

unsigned is a keyword. unsigned can only be used for integral types. unsigned or signed is considered a type specifier, a simple type specifier. Therefore, they indicate that the type will be either signed or unsigned.

You can typedef words unsigned int to uint , but then it will look like a type when int is a type and unsigned is a type specifier as such.

You can use unsigned yourself, contrary to popular belief, as evidenced by the C ++ ISO 7.1.5.2 standard. Table 7:

enter image description here

You can use several type specifiers (when allowed) and can freely mix with declarator pointers in any order.

You can also do this:

 int unsigned i; 

and this is really C ++.

+3
source

No unsigned is a keyword on its own; it is only a modifier for int, short, char.

The unsigned keyword can be used as a modifier for int , short , char or long . It can also be used as a type name; unsigned means unsigned int .

This was done so as to avoid the three additional keywords uint ushort uchar and because the signed / unsigned might not have been different on all the early computers that C and Unix were targeting.

+2
source

Keywords, signed, unsigned, short and long, are type modifiers / specifiers, and when one of these modifiers / specifiers of this type is used on its own, an int data type is assumed.

Thus, signed and unsigned can also be used as standalone type specifiers , which means the same as signed int and unsigned int respectively. The following two declarations are equivalent:

 unsigned abc; unsigned int abc; 
+1
source

There is also unsigned char. Some compilers, such as GNU g ++, allow you to simply put unsigned and assume that you mean unsigned int. For example, the following code is equivalent

 unsigned int x; unsigned x; 
0
source

Source: https://habr.com/ru/post/899150/


All Articles