Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'long unsigned' as valid as 'unsigned long' in C?

Tags:

c

types

A question was recently asked about whether ULL or LLU was valid for specifying unsigned long long constants in C. I know they're both valid but I was thinking that ULL would be preferable since it matched the type unsigned long long.

However, I'm not so certain now. The gcc compiler allowed the following without complaint:

int main(void) {
    unsigned int a = 1;
    int unsigned b = 2;
    return 0;
}

So my question is: are int unsigned, and other variations like long long unsigned, valid types according to the standard?

like image 836
paxdiablo Avatar asked Jun 25 '13 01:06

paxdiablo


People also ask

Is Long Long valid in C?

If long appears twice, int appears once or not at all, and the rest is valid (no appearance of other type names, no clashing or invalid specifiers, etc.) you have a long long int (signed if signed or nothing appeared, unsigned if unsigned appeared). Even if a bunch of other stuff is in between.

What is the difference between unsigned long and unsigned long long?

They're two distinct types, even if they happen to have the same size and representation in some particular implementation. unsigned long is required to be at least 32 bits. unsigned long long is required to be at least 64 bits.

Is unsigned int same as unsigned long?

Practically, this corresponds to 16 bit. Implementations (i.e. compilers) may provide a unsigned int with a larger range, but are not required to. In comparison, unsigned long int is guaranteed to be able to represent values in the range 0 to 4294967295 . Practically, this corresponds to 32 bit.

Is long int valid in C?

For instance, the long int, short int, unsigned int, signed int, etc., are all very valid data types in the C programming language.

What is unsigned long long int data type in C++?

In this article, we will discuss the unsigned long long int data type in C++. It is the largest (64 bit) integer data type in C++ . Some properties of the unsigned long long int data type are: An unsigned data type stores only positive values. It takes a size of 64 bits.

What is the difference between signed and unsigned integer in C?

A signed integer can store the positive and negative value both but beside it unsigned integer can only store the positive value. The range of nonnegative values of a signed integer type is a sub-range of the corresponding unsigned integer type. //Assuming the size of the integer is 2 bytes.

What is the minimum value that can be stored in unsigned long?

The minimum value that can be stored in unsigned long long int is zero. In case of overflow or underflow of data type, the value is wrapped around. For example, if 0 is stored in an unsigned long long int data type and 1 is subtracted from it, the value in that variable will become equal to 18, 446, 744, 073, 709, 551, 615.

Is Ull or LLU valid for unsigned long long constants in C?

A question was recently asked about whether ULL or LLU was valid for specifying unsigned long long constants in C. I know they're both valid but I was thinking that ULL would be preferable since it matched the type unsigned long long.


1 Answers

The ISO C11 standard states in 6.2.5 Types:

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int.

For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements.

However there's no mandate in that section as to the order in which the base type and unsigned modifier appears.

The controlling section is later in the standard, 6.7.2 Type specifiers, paraphrased here:

Type specifiers are void, char, short, int, long, float, double, signed, unsigned, _Bool, _Complex, <struct-or-union-specifier>, <enum-specifier>, and <typedef-name>.

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

It then goes on to list all the multisets, such as unsigned long, or unsigned long int.

But the important phrase there is the type specifiers may occur in any order, meaning that all of these are valid for that multiset:

unsigned long
long unsigned

unsigned long int
unsigned int long
long unsigned int
long int unsigned
int unsigned long
int long unsigned
like image 124
paxdiablo Avatar answered Oct 04 '22 08:10

paxdiablo