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
longtype.
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:
- The variable
populationis declared as alongand initialized with the value7800000000. - The
longtype is used to handle large values that exceed the range of theinttype. - The value is printed using the
coutstatement.
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:
- The variables
distanceEarthToSunanddistanceEarthToMarsare declared aslong. - The total distance is calculated by adding these two values and stored in
totalDistance. - The result is printed using the
coutstatement.
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:
- The variable
lightYearsis declared aslong longto store a very large value. - The
long longtype provides more storage capacity than thelongtype. - The value is printed using the
coutstatement.
Key Points about long Keyword
- The
longkeyword is used to declare integer variables with a larger range thanint. - On most systems,
longis 4 bytes (32 bits), whilelong longis 8 bytes (64 bits). - It can be combined with
unsignedto store only non-negative values, effectively doubling the upper range. - Use
longorlong longwhen dealing with large numerical values.
