In C, the long keyword is a datatype modifier that can be applied to the basic datatypes to increase the range and size of values their variables can store. For example, a long int variable can store integer values that range from -2,147,483,648 to 2,147,483,647 which is significantly greater as compared to the normal int data type, which can store integer values that range from -32,768 to 32,767 in the 16-bit compiler.
How to use long modifier?
We can apply the long modifier to the allowed data types just by adding it as a prefix in the normal variable declaration. For example:
int var; // normal declaration
long int var; // long int variable declaration
One thing to note here is that we cannot use the long modifier with any data type of our choice. In C, we can only use long with 3 datatypes which are as follows:
- long int
- long long int
- long double
1. long int
When long is applied to an integer it creates a long int, which increases the range of values that int can store. The long int is used when the range of values exceeds the int data type.
- The size of the long int is 4 bytes.
- It can store integer values that range from -2,147,483,648 to 2,147,483,647.
- The format specifier for long int is %ld.
- The long int literals can be represented by a numeric value followed by "L" or "l" as the suffix.
Syntax of long int
long int var1;
In C, we can skip the int keyword and just declare the variable with only long and it will be equivalent to long int declaration.
long var2;
Here, both var1 and var2 are of the same datatype and carry the same size.
Example of long int
C
// C program to illustrate long int
#include <stdio.h>
int main()
{
// declaring some int and long int variables
int ni;
long int li1;
long li2;
// assigning some big number to the variables
ni = li1 = li2 = 3000000000;
// printing valuees
printf("Values:\n\tint = %d\n\tlong int1 = %ld\n\tlong "
"int2 = %ld\n",
ni, li1, li2);
printf("Size:\n\tint = %d\n\tlong int1 = %ld\n\tlong "
"int2 = %ld\n",
sizeof(ni), sizeof(li1), sizeof(li2));
return 0;
}
Output
Values:
int = -1294967296
long int1 = 3000000000
long int2 = 3000000000
Size:
int = 2
long int1 = 4
long int2 = 4
As we can see, the long int variables can store larger numbers than int variables and also have a greater size than int.
Note: The size of the long int and other variables varies depending upon the compiler and system architecture.
2. long long int
We can even apply the long modifier to long int to further increase the size of the data type. The long long int is often used when the range of values exceeds the long data type.
- The size of the long long int is 8 bytes.
- It can represent integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- The format specifier for the long long data type is %lld.
- The long int literals can be represented by a numeric value followed by "LL" or "ll" as the suffix.
Syntax of long long int
long long int var;
As with the long int, we can also declare long long int by only using long long keywords.
long long var;
Example of long long int
C
// C program to illustrate the long long int
#include <stdio.h>
int main()
{
// declaring long long int variables using both ways
long long int var1;
long long var2;
// printing the size of long long it
printf("Size of var1: %d\nSizeof var2: %d",
sizeof(var1), sizeof(var2));
return 0;
}
OutputSize of var1: 8
Sizeof var2: 8
3. long double
We can use a long modifier with double datatype to create a long double variable. Long double is used to store significantly large decimal numbers which surpass the range of double.
- The size of a long double is 16 bytes.
- It provides more accuracy, precision and a larger range than the standard double data type.
- The format specifier for long double is %Llf.
Syntax of long double
long double var;
Example of long double
C
// C program to illustrate the long long int
#include <stdio.h>
int main()
{
// declaring the long long int variable
long double var;
// printing the size of long double
printf("Size of long double: %d", sizeof(long double));
return 0;
}
OutputSize of long double: 16
When to Use long in C?
The common practice while coding is to utilize the standard data type, except for scenarios when there is a need to contain values more than its capacity, in those cases the long data type is recommended. It's important to note that long data types demand more memory space unlike basic data types, thus caution should be exercised upon deciding when to use it, so you should only use it when necessary.
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read