Thread: What's the difference between long and long int?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    What's the difference between long and long int?

    What's the difference between long and long int?
    Similarly, what's the difference between short and short int?

    When I use typeid on them (using visual c++) they retain their types as 'long' and 'short' instead of being converted implicitly to 'long int' or 'short int' as I thought they would have.
    Code:
        long var = 5.5;
        std::cout << typeid(var).name();
    output: long

    long and short also occupy the same amount of space as long int and short int accoring to sizeof().

    So really how are they different, and why are long and short valid types when they're supposed to be modifiers? It's juzt confusing. Because I remember seeing long in examples (now forgotten where I saw them) and always thought that it was implicitly long int.

    I tried searching the web for a long time but didn't get any proper material about this.
    Last edited by Nwb; 02-15-2019 at 12:57 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    long means long int. They are exactly the same. Most programmers prefer to say long.
    short and short int are also exactly the same.
    So are long long and long long int.
    So are unsigned and unsigned int.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    But then why are we able to use long and short as data types at all? There's gotta be a reason for everything in C++..
    Besides they're also modifiers so it would be confusing to also use them as types..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But then why are we able to use long and short as data types at all?
    History - because that's what archaic C allowed you to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Hmmm would you happen to know why C allowed that? But thanks Salem, that makes sense.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It's allowed because there's no other reasonable interpretation of long or long long or unsigned or unsigned long or unsigned long long. Why force people to put int after all of those? It adds absolutely nothing.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Nwb View Post
    Hmmm would you happen to know why C allowed that? But thanks Salem, that makes sense.
    Because "long" was used before "long int" was supported in most C Compilers. It was likely done because the compiler was easier to write that way.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by john.c View Post
    It's allowed because there's no other reasonable interpretation of long or long long or unsigned or unsigned long or unsigned long long. Why force people to put int after all of those? It adds absolutely nothing.
    My point was that having two types (as I found that 'long' wouldn't be implicitly converted to 'long int') that basically are the same thing can cause confusion. Take the example of function overloading, how do you know which overload would be taken (keep in mind overloading is based on types not values)? And remember this was after I thought 'long' and 'long int' are different types, now I found that they're not.

    Turns out 'long int' is nothing about 'long'! So it's the other way around. I didn't know this at all.
    Anyways thanks everybody for contributing to the thread

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nwb
    My point was that having two types (...) that basically are the same thing can cause confusion.
    long and long int both denote the same type, so there is only one type. long is a simple type specifier for the type long int.

    Quote Originally Posted by Nwb
    (as I found that 'long' wouldn't be implicitly converted to 'long int')
    It is true that a long wouldn't be implicitly converted to long int, but that's because they are of the same type. It would be like trying to implicitly convert an int to an int.

    Quote Originally Posted by Nwb
    Take the example of function overloading, how do you know which overload would be taken (keep in mind overloading is based on types not values)?
    Good question. So, you're saying that you can compile this program without an error?
    Code:
    #include <iostream>
    
    void foo(long int x)
    {
        std::cout << "long int" << std::endl;
    }
    
    void foo(long x)
    {
        std::cout << "long" << std::endl;
    }
    
    int main()
    {
    }
    If so, you might want to change to a standard conforming compiler. You cannot overload with the corresponding parameter differing only by one being long and the other being long int because long and long int denote the exact same type.

    Quote Originally Posted by Nwb
    And remember this was after I thought 'long' and 'long int' are different types, now I found that they're not.
    Eh, you just wrote that they are two types, and now you say that they are the same type?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    I was explaining my initial assumption and my conclusion ;p
    I thought they were two different types. I found out that 'long int' is nothing but 'long'. And hence there's only one type.

    I get that it wasn't that clear, but I was trying to tell john.c why it would cause confusion for having two types that did the same thing. But then again it's not two types. It's one type. So ya I messed up that post.

    You see, I was about to post that when I decided to test whether 'long int' was 'long int' and then when I found out that it was 'long', I lost my train of thought and posted that saying two contradicting things.
    Last edited by Nwb; 02-16-2019 at 05:54 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Don't forget there are such things as 'long double' as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    > But then why are we able to use long and short as data types at all?
    History - because that's what archaic C allowed you to do.
    Depending on the platform, int and long int are completely different "types". On Windows, both have the same size (32 bits), but on SysV ABI for x86-64 int is 32 bits long, and long int, 64.

    On 64 bits architectures there are different possible combinations of sizes denoted by I32LP64, IL32P64, ILP64 and SILP64. "I" here is "int", "L" is "long", "P" is "pointer" and "S" is short. There are some systems where 'char' is NOT 8 bits long...

    Windows uses IL32P64. SysV ABI (Unixes on x86-64) use I32LP64. The other two are rare (see 64-bit computing - Wikipedia.

    If you are using GCC, some symbols are predefined and you can use them to test for a "type" size:
    Code:
    $ gcc -dM -E - < /dev/null | grep SIZEOF
    #define __SIZEOF_FLOAT80__ 16
    #define __SIZEOF_INT__ 4
    #define __SIZEOF_POINTER__ 8
    #define __SIZEOF_LONG__ 8
    #define __SIZEOF_LONG_DOUBLE__ 16
    #define __SIZEOF_SIZE_T__ 8
    #define __SIZEOF_WINT_T__ 4
    #define __SIZEOF_PTRDIFF_T__ 8
    #define __SIZEOF_FLOAT__ 4
    #define __SIZEOF_FLOAT128__ 16
    #define __SIZEOF_SHORT__ 2
    #define __SIZEOF_INT128__ 16
    #define __SIZEOF_WCHAR_T__ 4
    #define __SIZEOF_DOUBLE__ 8
    #define __SIZEOF_LONG_LONG__ 8
    You can also use the header limits.h where some symbols are defined to test the limits of a integral type.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by flp1969 View Post
    Depending on the platform, int and long int are completely different "types". On Windows, both have the same size (32 bits), but on SysV ABI for x86-64 int is 32 bits long, and long int, 64
    Pure idiocy, responding to a question that wasn't asked.
    Nobody said that int and long were the same.
    Just that long and long int are the same.
    Jesus!
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Depending on the platform, int and long int are completely different "types".
    No, they ARE different types - period.
    If they happen to have the same number of bits and representation on a platform doesn't make them suddenly the same type.
    Equivalence in storage capacity doesn't make for equality of type.

    Quote Originally Posted by C99
    6.3.1 Arithmetic operands
    6.3.1.1 Boolean, characters, and integers
    1 Every integer type has an integer conversion rank defined as follows:
    — No two signed integer types shall have the same rank, even if they have the same
    representation.
    — The rank of a signed integer type shall be greater than the rank of any signed integer
    type with less precision.
    — The rank of long long int shall be greater than the rank of long int, which
    shall be greater than the rank of int, which shall be greater than the rank of short
    int, which shall be greater than the rank of signed char.
    Code:
    int a = 1;
    long int b = 2L;
    long int c = a + b;
    So 'a' will always be promoted to long int before the addition (during semantic parsing), even if that eventually turns into a nop at code generation time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    By the way.. how is this function call not ambiguous?
    Code:
    #include <iostream>
    
    void print(int) {
        std::cout << "Int\n";
    }
    
    void print(long long int) {
        std::cout << "Long\n";
    }
    
    int main()
    {
        std::cout << "Type of literal: " << typeid(5l).name();
        std::cout << "\nFunction called: ";
        print(5l);
        std::cin.get();
    }
    Short also calls the 'int' overload. How does the compiler know that 'short int' must call 'int' instead of 'long' (I wrote another overload for long)?
    Last edited by Nwb; 02-21-2019 at 01:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-05-2018, 09:59 AM
  2. Replies: 16
    Last Post: 06-07-2015, 03:28 PM
  3. Difference between 'int' and 'long int'
    By cplusplusnoob in forum C++ Programming
    Replies: 15
    Last Post: 03-26-2012, 01:42 PM
  4. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  5. whats the difference between int and long?
    By orion- in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 08:18 PM

Tags for this Thread