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:
- The variable
population
is declared as along
and initialized with the value7800000000
. - The
long
type is used to handle large values that exceed the range of theint
type. - 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:
- The variables
distanceEarthToSun
anddistanceEarthToMars
are declared aslong
. - The total distance is calculated by adding these two values and stored in
totalDistance
. - 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:
- The variable
lightYears
is declared aslong long
to store a very large value. - The
long long
type provides more storage capacity than thelong
type. - The value is printed using the
cout
statement.
Key Points about long
Keyword
- The
long
keyword is used to declare integer variables with a larger range thanint
. - On most systems,
long
is 4 bytes (32 bits), whilelong long
is 8 bytes (64 bits). - It can be combined with
unsigned
to store only non-negative values, effectively doubling the upper range. - Use
long
orlong long
when dealing with large numerical values.