longint

All variables use data type during declarations lớn restrict the type of data lớn be stored. Therefore, we can say that data types are used lớn tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Different data types require a different amount of memory.

Bạn đang xem: longint

Integer: The từ khóa used for integer data types is int. Integers typically require 4 bytes of memory space and range from -2147483648 lớn 2147483647.

Datatype Modifiers: As the name implies, datatype modifiers are used with the built-in data types lớn modify the length of data that a particular data type can hold. 

Below is a list of ranges along with the memory requirements and format specifiers on the 32-bit GCC compiler.

S. No. 

Data Type

Memory

(bytes)

Range

1int4-2^31 lớn 2^31- 1
2Long int4-2^31 lớn 2^31 – 1
3Long long int8-2^63 lớn 2^63 – 1

 Long long takes the double memory as compared lớn long. But it can also be different on various systems. Its range depends on the type of application. The guaranteed minimum usable bit sizes for different data types:

  • char: 8
  • short:16
  • int: 16
  • long: 32
  • long long: 64

The decreasing order is: long long >=long>=int>=short>=char

Program 1:

In various competitive coding platforms, the constraints are between 107 lớn 1018. Below is the program lớn understand the concept:

C++

#include <iostream>

using namespace std;

int main()

{

    int p = 100000;

    int q = 100000;

    int result = p * q;

    cout << result << endl;

    return 0;

}

As above, the output is not correct as int can’t store a 1010 value(out of its range). In this case, long long should be used.

Program 2:

Below is the C++ program lớn demonstrate how converting int lớn long long affects the output:

Xem thêm: nguyên nhân trực tiếp dẫn đến chiến tranh thế giới thứ hai

C++

#include <iostream>

using namespace std;

int main()

{

    int p = 100000;

    int q = 100000;

    long long int result = p * q;

    cout << result << endl;

    return 0;

}

Explanation: The above program gives the same output even after converting int lớn long long int because at first, the result is declared as long long. But before assigning the value of multiplication of p and q, it is already overflowed. Now, lớn prevent the overflow condition, it is required lớn convert the int result lớn the long long int before assigning the result value so sánh that the overflow condition does not occur.

Program 3:

Below is the C++ program lớn implement the above approach:

C++

#include <iostream>

using namespace std;

int main()

{

    int p = 100000;

    int q = 100000;

    long long int result = (long long int)p * q;

    cout << result << endl;

    return 0;

}

Explanation: After this, it gives the correct output, which is 1010, which can be easily stored into a long long data type as the range is up lớn 1018.

Difference between long int and long long int in C/C++ in tabular form:

Feature                            long int                    long long int
Size4 bytes on most systems.8 bytes on most systems.
Syntaxlong int x;long long int x;
Range-2^31 lớn 2^31 – 1-2^63 lớn 2^63 – 1
Format specifier%li%lli
UsageUsed for storing integers with medium range.Used for storing integers with large range.

Last Updated : 07 May, 2023

Like Article

Save Article

Xem thêm: top 10 đỉnh núi cao nhất việt nam