23

Is unsigned long int equivalent to unsigned long in C++?

In my opinion they are same. But I've seen some people still using unsigned long int in code and I don't understand why. Here is a sample code:

#include <stdio.h> int main() { unsigned long int num = 282672; int normalInt = 5; printf(""); return 0; } 
0

5 Answers 5

28

Yes.

long is just a shorthand for long int. This is because in principle long is just a qualifier (it can also be used for prolonging a double datatype, for example)

From C++ ISO Standard, section 7.1.5.2, a table of equivalent type specifiers:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I see that in your table every type omits the signed token, except for chars. Is this intentional or is this a mistake in the table?
@RikSchaaf - No it is not a mistake. The standards specify that it is implementation defined whether char is a signed or unsigned type. And, in fact, char is actually a distinct type from both unsigned char and signed char. This is different from all other integral types, which are signed unless the unsigned keyword is used.
@Sebastian Interesting to know. I read a bit through the answers and comments of the 2 questions to see the reason why both exist. Thanks for the clarification.
4

§6.7.2 of the C99 standard gives the following list of types (this is only an excerpt):

  • short, signed short, short int, or signed short int
  • unsigned short, or unsigned short int
  • int, signed, or signed int
  • unsigned, or unsigned int
  • long, signed long, long int, or signed long int
  • unsigned long, or unsigned long int
  • long long, signed long long, long long int, or signed long long int
  • unsigned long long, or unsigned long long int

with the following additional point:

(5) Each of the comma-separated sets designates the same type, except that for bit-fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.

5 Comments

Though this is a C++ question ;)
Yes, C++ question with code that includes stdio.h and uses printf ;)
Right :P Sometimes, I look at just the tags, sometimes at just the code, and fall prey to this, too :D
The question is tagged both C and C++ anyway.
It wasn't initially. The C tag was added more than a year later.
1

Yes. unsigned, signed, short, long, long long all are simple type specifiers for XXX int.

See 7.1 Specifiers [dcl.spec] in the standard:

3 [ Note: Since signed, unsigned, long, and short by default imply int, a type-name appearing after one of those specifiers is treated as the name being (re)declared. [ Example:

void h(unsigned Pc); // void h(unsigned int)

void k(unsigned int Pc); // void k(unsigned int)

—end example ] —end note ]

and 7.1.6.2 Simple type specifiers [dcl.type.simple]

 Table 10 — simple-type-specifiers and the types they specify Specifier(s) | Type ------------------------+--------------------------------- type-name | the type named simple-template-id | the type as defined in 14.2 char | “char” unsigned char | “unsigned char” signed char | “signed char” char16_t | “char16_t” char32_t | “char32_t” bool | “bool” unsigned | “unsigned int” unsigned int | “unsigned int” signed | “int” signed int | “int” int | “int” unsigned short int | “unsigned short int” unsigned short | “unsigned short int” unsigned long int | “unsigned long int” unsigned long | “unsigned long int” unsigned long long int | “unsigned long long int” unsigned long long | “unsigned long long int” signed long int | “long int” 

Comments

0

unsigned long int is the correct type definition, however int can be ignored.

1 Comment

As are long unsigned and int long unsigned.
0

Yes, they are the same. Saying unsigned long int is simply explicitly stating that it is an int.

You can always look at the size of the type by sizeof(unsigned long int) and sizeof(unsigned long)

Hope this helps.

1 Comment

I'm not sure what you mean by "explicitly stating that it is an int". unsigned long int and int are two distinct types. int is both a keyword and the name of a type; the name unsigned long int uses the keyword int, but does not refer to the type int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.