Open In App

C Long

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:

  1. long int
  2. long long int
  3. 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.



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 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.

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 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;
}

Output
Size 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. 

Syntax of long double

long double var;

Example of long double




// 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;
}

Output
Size 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.


Article Tags :