Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the actual use of "signed" keyword?

Tags:

c++

I know that unsigned integers are only positive numbers (and 0), and can have double the value compared to a normal int. Are there any difference between

int variable = 12; 

And:

signed int variable = 12; 

When and why should you use the signed keyword?

like image 702
Erik W Avatar asked Jan 18 '15 17:01

Erik W


People also ask

What is the use of signed keyword in C?

signed keyword is used for those variables which can take positive as well as negative values. unsigned keyword is used for those variables which can take only values which are zero or positive i.e., without - (negative sign). We can use signed and unsigned keywords with only 'int' and 'char' data types.

What is the use of signed character?

You would use a signed char when you need to represent a quantity in the range [-128, 127] and you can't (for whatever reason) spare more than a single byte to do it.

What is signed and unsigned keyword in C?

The term "unsigned" in computer programming indicates a variable that can hold only positive numbers. The term "signed" in computer code indicates that a variable can hold negative and positive values. The property can be applied to most of the numeric data types including int, char, short and long.

What is int16_t in C?

For example, the name int16_t indicates a 16-bit signed integer type and the name uint32_t indicates a 32-bit unsigned integer type. To make these names available to a program, include the inttypes. h header file.


2 Answers

There is only one instance where you might want to use the signed keyword. signed char is always a different type from "plain" char, which may be a signed or an unsigned type depending on the implementation.

C++14 3.9.1/1 says:

It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types [...]

In other contexts signed is redundant.


Prior to C++14, (and in C), there was a second instance: bit-fields. It was implementation-defined whether, for example, int x:2; (in the declaration of a class) is the same as unsigned int x:2; or the same as signed int x:2.

C++11 9.6/3 said:

It is implementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int, long, or long long bit-field is signed or unsigned.

However, since C++14 this has been changed so that int x:2; always means signed int. Link to discussion

like image 104
CB Bailey Avatar answered Sep 30 '22 15:09

CB Bailey


In the case of int, there's no difference. It only makes a difference with char, because

  1. it is not defined whether char is signed or unsigned, and
  2. char, signed char, and unsigned char are three distinct types anyway.

So you should use signed if you need a signed char (which is probably rarely). Other than that, I can't think of a reason.

like image 39
Wintermute Avatar answered Sep 30 '22 16:09

Wintermute