What is the difference between int type, long type and long type in C++ language?

Asked 2 years ago, Updated 2 years ago, 73 views

I would like to know the difference between int type, long type and long type in C++.
In particular, I would like to know about the upper limit values that can be expressed, so I looked into them and found out that the following is the recognition.

int->2^31-1
long->2^31-1
long long->2^63-1

I understand the difference between int and long, but I don't know how to position long.
Both long and long are 2^63-1.In the video of Atcoder's explanation, you explained that if int is not enough, use long long, but isn't it long after int?
The background is that when I looked into it, I couldn't understand it all the better.

I would appreciate it if you could explain the difference between long and long in terms of the upper limit.
It may depend on the version, so I will share that I use c++11.

I look forward to your kind cooperation.

c++ c++11

2022-09-30 21:33

4 Answers

is an ancient language. displays important questions with Now that you know about it,

Language Standards ISO/IEC 9899:1999 specifies

  • char must be at least 8 bits int or smaller
  • int must be at least 16 bits in size
  • long must be at least 32 bits long or smaller
  • long must be at least 64bit in size

I'm still using it as a matter of course in my embedded 8/16bit microcomputer to meet the previous requirements

  • char8bit
  • int16bit
  • long32bit

Same for 32-bit microcomputers or advanced 32-bit processors such as x86

  • char8bit
  • short16bit
  • int32bit

The specific size of long or long depends on the processor's own designer, compiler designer, and OS's own specifier.The criteria for this decision are

  • Easy to bring out hardware performance (I want to write a 32-bit exclusive source)
  • Easy to port existing software (don't want to touch sources from the 16-bit era)

The opposite is true, and after many twists and turns, many 32-bit processor compilers have

  • long 32-bit (to ensure compatibility with 16-bit source code)
  • long64bit (because longmust be larger)

is calm (as stated in your question).

Therefore, the answer to your question will be, "This size has been adopted as a result of choosing a model size that is easy to port old source code."

"Compatibility with existing software" is particularly important for 64-bit processors, and different specifications exist side by side for the size of the model. Like LP64 or LLP64.
https://project-flora.net/2015/07/21/cc%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%E6%95%B4%E6%95%B0%E5%9E%8B%E3%81%AB%E3%81%AF%E6%B0%97%E3%82%92%E3%81%A4%E3%81%91%E3%82%88/a>

And it's very inconvenient that the size of the type varies from compiler to compiler. or

Visual C++ and Windows,

int32bit
long32bit
long64bit

It is stipulated that
As far as AtCorder is concerned, the Language Test page is

Note: See below for information on AtCoder Judge servers

$uname-a
Linux ip-***-***-***-*** 3.13.0-74-generic#118-Ubuntu SMP Thu Dec 17 22:52:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ cat/etc/lsb-release
DISTRIB_ID = Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"

It appears that you are using Linux x86_64.Linux has Linux Standard Base and Linux Standard Base Core Specification for X86-64/Chapter 7. Low Level System Information/7.1. Machine Interface/7.1.2.Data Representation/7.1.2.3. Fundamental Types

LSB-conforming applications shall use only the fundamental types described in Section 3.1.2 of System V Application Binary Interface AMD64 Architecture Processor Supplement.

System V Application Binary Interface AMD64 Architecture Processor Supplement/Chapter 3 Low Level System Information/3.1.2 Data Representation/Fundamental Types specifies

int32bit
long64bit
long64bit

It is stipulated thatAs you can see in the same way, Linux x86 specifies long as 32-bit, so long is different in size between x86 and x86_64.


2022-09-30 21:33

The upper limit (bit width) of each integer type in C/C++ depends on the system (of course sizeof(int)<=sizeof(long)<=sizeof(long) is always present for each system).

Basic Type - According to cppreference.com, specifically

  • int 16-bit or 32-bit
  • long32-bit or 64-bit
  • long64-bit

It may have a width of .Therefore, you can see that long is not always greater than int.On the other hand, long is guaranteed to have a 64-bit width, so it is always larger than int and the policy of "use long long if int is not enough" is always valid.

As far as AtCoder is concerned at least, it is safe to think that int=32 bits, long=64 bits.If you are worried, you may want to use a type that specifies the bit width, such as int32_t or int64_t.

By the way, AtCoder uses GCC, but GCC defines an integer with a 128-bit width of _int128, which can handle situations where even 64 bits are insufficient.


2022-09-30 21:33

The int, long and long types are defined in limits.h, so check the header file of your C++ compiler.
A special integer type may be defined in "stdint.h", so be sure to check it.

The difference between int and long and long depends on the computer environment (the number of bits in the CPU).
The int type is often an integer with the same number of bits as the CPU, long is a 32-bit integer, and long is a 64-bit integer. (The term "integer" is used to mean both integers and unsigned integers.)
In the table, the CPU type (number of bits) and the number of int, long, and long bits of each are as follows:

 int long long long long
  16bit CPU 163264
  32-bit CPU 323264
  64-bit CPU646464


2022-09-30 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.