C++ long Keyword

The long keyword in C++ is used to declare integer variables with a larger storage size than the int data type. It allows for storing larger values and is typically used when the range of int is insufficient for the application.

The size of long is generally platform-dependent but is often 4 bytes (32 bits) on 32-bit systems and 8 bytes (64 bits) on 64-bit systems. It can be combined with signed or unsigned and can also be extended with long long for even greater ranges.


Syntax

</>
Copy
long variable_name = value;
long long variable_name = value;
long
The keyword used to declare a long integer variable.
variable_name
The name of the variable being declared.
value
An optional initial value for the variable. It must be an integer within the range of the long type.

Examples

Example 1: Declaring and Initializing a long Variable

In this example, you will learn how to declare and initialize a long variable and display its value.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    long population = 7800000000; // World's population
    cout << "World population: " << population << endl;
    return 0;
}

Output:

World population: 7800000000

Explanation:

  1. The variable population is declared as a long and initialized with the value 7800000000.
  2. The long type is used to handle large values that exceed the range of the int type.
  3. The value is printed using the cout statement.

Example 2: Performing Arithmetic with long

In this example, you will learn how to use long variables for arithmetic operations.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    long distanceEarthToSun = 149600000; // Distance in kilometers
    long distanceEarthToMars = 225000000; // Distance in kilometers

    long totalDistance = distanceEarthToSun + distanceEarthToMars;

    cout << "Total distance: " << totalDistance << " km" << endl;
    return 0;
}

Output:

Total distance: 374600000 km

Explanation:

  1. The variables distanceEarthToSun and distanceEarthToMars are declared as long.
  2. The total distance is calculated by adding these two values and stored in totalDistance.
  3. The result is printed using the cout statement.

Example 3: Using long long for Extremely Large Numbers

In this example, you will learn the use of long long to handle even larger values.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    long long lightYears = 9460730472580800; // Distance light travels in one year
    cout << "Light years in meters: " << lightYears << endl;
    return 0;
}

Output:

Light years in meters: 9460730472580800

Explanation:

  1. The variable lightYears is declared as long long to store a very large value.
  2. The long long type provides more storage capacity than the long type.
  3. The value is printed using the cout statement.

Key Points about long Keyword

  1. The long keyword is used to declare integer variables with a larger range than int.
  2. On most systems, long is 4 bytes (32 bits), while long long is 8 bytes (64 bits).
  3. It can be combined with unsigned to store only non-negative values, effectively doubling the upper range.
  4. Use long or long long when dealing with large numerical values.