Open In App

C Long

Improve
Improve
Like Article
Like
Save
Share
Report

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.

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


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. 

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


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.



Last Updated : 14 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads