Integer Data Type with Long Modifier

In this tutorial, we will learn about the long modifier and its significance when used with integer data types.


What is a Modifier?

A modifier is used to change or modify the type of something. There are different types of modifiers that can be used with integers:

  • short
  • long
  • long long

These modifiers can be added before an integer type to change its default size and range.
The detailed explanation of short modifier is available in this article.


Long Modifier

The long modifier is used to increase the size of an integer type from its default size.

Using the long modifier with a signed integer

Syntax:

long int variable_name;

Or

int long variable_name;

Let’s consider the following program:

#include <stdio.h>

int main()
{
    long int var = -40845874;
    printf("The integer value stored in variable var is %ld and ", var);
    printf("the size of long integer in my machine is %d bytes", sizeof(var));
}

Output:

Please note that the size of a long int on my machine is 4 bytes, but it may be 8 bytes on your machine. The size depends on the compiler. However, keep in mind the following fact:

Fact
The size of a long int may not be greater than the size of an int on some machines, but one thing is certain: the size of an int can never be greater than the size of a long int.

You can calculate the range of a long int using the following formula:

where n represents the number of bits.

Therefore, a long int with 4 bytes (32 bits) has the following range:

Using the long modifier with an unsigned integer

Syntax:

unsigned long int variable_name;

Or

unsigned int long variable_name;

Let’s consider the following program:

#include <stdio.h>

int main()
{
    unsigned long int var = 907243902;
    printf("The integer value stored in variable var is %lu and ", var);
    printf("the size of unsigned long integer in my machine is %d bytes", sizeof(var));
}

Output:

The range of unsigned long int type of 4 bytes (32 bits) is



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.