rwallace
rwallace

Reputation: 33515

Can int and long be the same type?

Of course int and long are not guaranteed to be the same type, but on a platform such as Windows when compiling in 32-bit mode where they happen to be the same size, is the compiler allowed to regard them as the same type?

I'm not sure how to definitively test whether the compiler regards two types as the same, but I tried a heuristic test with int *a; long *b = a; and the Microsoft compiler accepts this without complaint whereas GCC gives a warning. Is either compiler behaving improperly or is it implementation dependent whether they name two different types or just one?

Upvotes: 2

Views: 294

Answers (2)

Christian Rau
Christian Rau

Reputation: 45968

Ok, the C99 standard says in section 6.5.6 Types:

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

And furthermore:

The type char, the signed and unsigned integer types, and the floating types are collectively called the basic types. Even if the implementation defines two or more basic types to have the same representation, they are nevertheless different types.

Which makes it pretty clear that they have to be distinct types, even if in C, which doesn't have templates, function overloading and typeid it may not be that practically important as in C++, where those types being the same type would definitely break much code.

EDIT: In fact because of this lesser practical relevance of type distinctness in C and the absence of the above mentioned language features, I cannot come up with a reliable way to test the absolute distinctness of two types right away.

Upvotes: 3

Daniel Fischer
Daniel Fischer

Reputation: 183978

The standard (N1570 draft of C2011 standard) says in 6.2.5 (4):

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

and in paragraph 14 of the same section:

The type char, the signed and unsigned integer types, and the floating types are collectively called the basic types. The basic types are complete object types. Even if the implementation defines two or more basic types to have the same representation, they are nevertheless different types.

so that says that int and long (another way of designating long int) are different types. They may have the same representation, behaviour, and alignment requirements, but they are not the same type as far as the language is concerned.

Upvotes: 5

Related Questions