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.
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.
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.
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.
Does omitting
signed
automatically default to signed variable in C?
Yes, usually, but no in general. Exceptions:
A char
is a distinct type from unsigned char
, and signed char
. char
will have the same range as unsigned char
or signed char
.
wchar_t
follows a similar 3-ness like char
.
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 5A 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.