Is there a difference between:
int a = 8; and
signed int a = 8; And is there a case when signed is required? in C and C ++.
signed adds difference only in relation to char , i.e. signed char and char are different types, while signed int and int are the same. Usually they do not need to be used, but instead use the proper type alias.
signed a = 8; - AbyxIn C, the signed keyword has effect in two contexts:
Applied to char type. In this case, unsigned char , signed char and char are three different types and it is not guaranteed that char is of sign type.
signed char a; // <- знаковый целый тип unsigned char b; // <- беззнаковый целый тип char c; // <- знаковость определяется реализацией Applied to a bit field. Without signed not guaranteed that the bit field has a sign representation, even if a sign type was used in the field declaration. The sign of the bit fields declared without explicitly signed or unsigned is determined by the implementation
signed int a : 2; // <- знаковое поле unsigned int b : 2; // <- беззнаковое поле int c : 2; // <- знаковость определяется реализацией In C ++, the second item does not hold. The sign of bit fields in C ++ follows the general rules, i.e. matches the character type used in the declaration.
That is, if you do not want your code to be tied to the implementation features, in these cases you need to specify signed to get a signed type or representation. In other cases, adding signed to the description of an integer type does not affect the type.
Source: https://ru.stackoverflow.com/questions/980282/
All Articles