7

I'm curious why the signed keyword exists because I believe most integers and other variables are signed by default. Putting the signed keyword before int virtually does nothing. What is it used for, and why does it exist?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Comyar D
  • 172
  • 2
  • 9
  • If you have an _unsigned_ value to start, you may wish to _force_ a signed value during calculation (e.g.) `unsigned int x = 0xFFFFFFFF; int y = 7; x = ((signed) x) + y;` There are other [probably better] examples. – Craig Estey Nov 24 '20 at 19:46
  • 2
    Even when it is a no-op, it's not "doing nothing": it's communicating the programmer's intent. – Lee Daniel Crocker Nov 24 '20 at 19:48
  • 4
    @CraigEstey I believe `(signed)` is equivalent to `(int)` ? – Eugene Sh. Nov 24 '20 at 19:48
  • Even more curious is the `auto` keyword. I cannot think of an example where it is necessary in C. (answer here: https://stackoverflow.com/a/23406251/4593267 ) – chqrlie Nov 24 '20 at 19:52
  • Note that `auto` does have a use, infers an unspecified type from the variable's usage, in modern C++. – user4581301 Nov 24 '20 at 19:53
  • 1
    @chqrlie note that this is tagged as both C and C++, and imho this is a case where that is fine, but `auto` is very different in C and C++ – 463035818_is_not_an_ai Nov 24 '20 at 19:54
  • @EugeneSh. I.A.N.A.L.L but I think that `signed` by itself says convert the base type to signed (e.g. `unsigned int` --> `int` but `unsigned long long` --> `long long`). What' more common is the reverse: `int x; (unsigned) x ...` I _think_ there was a question recently from P__J about UB and signedness that clarified this https://stackoverflow.com/questions/64899337/does-x-x-x-where-x-is-an-unsigned-integer-invoke-ub – Craig Estey Nov 24 '20 at 19:55
  • 1
    @CraigEstey: I'm afraid casting an `unsigned long long` as `(signed)` converts it to an `int`, not a `long long`. – chqrlie Nov 24 '20 at 19:57
  • @chqrlie After rereading the link I was referring to, it doesn't seem to cover casting, only unary `-` – Craig Estey Nov 24 '20 at 20:02

2 Answers2

15

There are at least two places where the signed keyword is not a no-op:

  • With char: the signedness of "plain" char is implementation-defined. On implementations where it's an unsigned type, signed char is needed to get the signed variant. Even if char is a signed type, signed char, char, and unsigned char are all distinct types.

  • With bitfields: bitfield members without explicit signedness have implementation-defined signedness. For example, in

    struct foo {
        int b:1;
    };
    

    the range of values of b may be { -1, 0 } or { 0, 1 } depending on the implementation. If you want to be sure you get the signed version, you need the signed keyword. Note that while the standard is not very clear on this, on popular implementations, this applies to typedef too: if the bitfield member uses a typedef-defined type that doesn't include explicit signedness, the implementation-defined signedness (on GCC, set by -fsigned-bitfields) applies there too. This means types like int32_t should be defined using the signed keyword to avoid really bad surprise behavior when they're use in bitfields.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Re “This means types like int32_t should be defined using the signed keyword to avoid really bad surprise behavior when they're use in bitfields”: Yikes! – Eric Postpischil Nov 24 '20 at 22:26
4

char is either signed or unsigned, but in any case it is a type distinct from unsigned char and signed char. Those three are different types:

char
signed char
unsigned char

If not with signed there would be some other way needed to distinguish them.

Even without char. Why not? It allows to be explicit:

signed int x; // Someone decided that x 
              // must be signed
int y;        // Did the author choose signed 
              // consciously? We cannot tell.
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    `signed int x;` // Someone copy-pasted the declaration from somewhere and was too scared to remove the magic `signed` keyword – anatolyg Nov 24 '20 at 19:52
  • @anatolyg meh I agree with your point, but there is cargo cult everywhere, thats no reason to try to write code that actually means what it says ;) – 463035818_is_not_an_ai Nov 24 '20 at 19:53