22

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;
}
Community
  • 1
  • 1
Anders Lind
  • 4,542
  • 8
  • 46
  • 59

5 Answers5

26

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

  • 1
    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? – Rik Schaaf May 27 '15 at 22:27
  • 1
    Nope, that's no mistake. See also http://stackoverflow.com/a/2054941/478658 and http://stackoverflow.com/a/87648/478658 – Sebastian Sep 18 '16 at 06:00
  • @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. – Peter Aug 12 '17 at 00:28
  • @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. – Rik Schaaf Feb 03 '18 at 08:51
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.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
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”
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

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

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
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.

NX1
  • 68
  • 3
  • 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`. – Keith Thompson Oct 16 '13 at 17:35