3

Is long == signed long (int, char, etc)?

Is this guaranteed by the specification or is there some obscure version or compiler that will blow up if I take this for granted.

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • No. not with bit fields. – chux - Reinstate Monica Mar 11 '18 at 05:38
  • 1
    @chux Wise master Yoda, I would really appreciate if you spoke in less cryptic tongue. My young padawan mind cannot comprehend your great brilliance. – AlanSTACK Mar 11 '18 at 05:40
  • A bit field is a special case which allows you to specify how many bits are required for the range of values you want to store in a variable. The top-rated answer to this is correct, but if you are using bit fields (you can google this - they have a very specific syntax) then there seems to be implementation-defined behavior associated with signed/unsigned interpretation. – Stephano Mar 11 '18 at 05:51
  • [enum's signness is also implementation defined](https://stackoverflow.com/q/2579230/995714) – phuclv Mar 11 '18 at 05:52
  • Not mentioned yet, bit-fields may either be signed or unsigned if not explicitly specified – M.M Mar 11 '18 at 05:59
  • @AlanSTACK [re](https://stackoverflow.com/users/2410359/chux) Yoda sleep he must too. – chux - Reinstate Monica Mar 11 '18 at 13:49

3 Answers3

6

Yes for all the types you list, other than char.

char is signed or unsigned in an implementation defined manner. Furthermore, char is an entirely distinct type from singed char and unsigned char, even though it will have the exact same properties as one of them.

You may think I'm talking crazy here, but here it is straight from the C standard:

6.2.5 Types - p15

The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

Yes following two variable definitions are same:

long x = 9;
signed long x = 9;

signed keyword is unnecessary here because variables are signed by default except char. If you need to use unsigned then you need to explicitly use unsigned keyword.

Destructor
  • 14,123
  • 11
  • 61
  • 126
1

Does omitting signed automatically default to signed variable in C?

Yes, usually, but no in general. Exceptions:

  1. A char is a distinct type from unsigned char, and signed char. char will have the same range as unsigned char or signed char.

  2. wchar_t follows a similar 3-ness like char.

  3. Bit fields.

Key citations about bit fields indicate an int bit field may be signed or unsigned. It is implementation defined behavior. See also. The use of long, unsigned long and unsigned long if allowed, would incur implementation defined behavior.

A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type. C11dr §6.7.2.1 5

A bit-field is interpreted as having a signed or unsigned integer type consisting of the specified number of bits.125 §6.7.2.1 10

125 As specified in 6.7.2 above, if the actual type specifier used is int or ..., then it is implementation-defined whether the bit-field is signed or unsigned.

  1. Other exceptions may exist - yet I do not know of them.
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256