×

Long int in C

  • We use data type to avoid the type of data to be stored for a variable at the time of declaration.
  • So, we can say that the variable type is known by the data type assigned to it and what type of data the variable can store.
  • As we know, there are different data types, so the memory allocation differs from one another. The memory is created by the compiler when any of the variables is declared.
  • The memory allocation depends on the data type we declared for a variable. The size also depends on the system on which it is operating.  Which takes 4 – bytes or sometimes it takes 2 – bytes.
  • Let us consider a list of ranges for memory requirements for a 32 – bit compiler
Sl no            Data Type       Bytes             Range
1                int             4                 - 2 ^ 31 to 2 ^ 31 – 1
2                Long int        4                 - 2 ^ 31 to 2 ^ 31 – 1
3                Long Long int   8                 - 2 ^ 63 to 2 ^ 63 - 1

The order of size is:

Char <= short <= int <= long <= long long
  • The size of the long int is between -2147483648 to 2147483647. This means the size of long int cannot go beyond this limit.
  • Let us consider a program for this scenario.
/ /  program in to implement
/ / the above approach
# include <stdio.h>
/ / main function
int main ( ) {
/ / declaring the variables
int a ,b , c ;
a = 99999;
b = 99999;
/ / performing the multiplication operation
c = a * b;
/ / printing the result
printf(“ The result is %d “, c );
return 0;
}

Output:

The result is 1539894908
  • As you can see, that output is not correct because it cannot store values out of range 10 ^ 10. So we have use increase the range to long int.

Let us consider a program where we use a long variable to print the result:

/ / program in c to implement the above scenario
# include <stdio.h>
/ / main function
int main (  ) {
/ / declaring int variables
int a, b;
/ / declaring long variable
long c;
 a = 99999;
b = 99999;
/ / performing the multiplication operation
c = a * b;
/ / print the result
printf(“ the result is %ld “ ,c );
return 0;
}

Output:

The result is 1539894908
  • We can see the output is still the same even after converting to long because before assigning the result of multiplication of a and b, it is overflowed.
  • So must to convert int results to long before assigning the result value.

Let us consider a program to implement the above scenario:

/ / c program to implement
/ / above approach
# include<stdio.h>
/ / main function
int main ( ) {
/ / declare the variables
long a, b, c;
a = 99999;
b = 99999;
/ / performing the operation
c = a * b ;
 / / printing the result
printf(“ the result is %ld” , c);
return 0;
}

Output:

The result is 9999800001
  • In the above case, the output printed is correct because the range is sufficient for long to store.
  • In a long int data type, its size is 4.
  • The sizeof( ) function is used to find the size of the data type.
  • Let us consider a program to find the size of long int data type using sizeof( ) operator.
/ / c program to implement
/ / above case
# include <stdio.h>
/ / main function
Int main ( ) {
/ / declaring int variable
Int a;
/ / declaring long variable
Long b;
Printf(“ enter a value for a: \n”);
Scanf (“%d”, &a);
Printf(“ enter a value for b :\n”);
Scanf(“%ld” , &b);
/ / displaying the size of datatype
Printf(“ the size of %d datatype is %lu byte \n”,a,sizeof( int) );
Printf(“ the size of %d datatype is %lu byte \n”, b ,sizeof( long) );
return 0;
}

Output:

Enter a value for a: 2
Enter a value for b:99
The size of a datatype is 4 byte
The size of b datatype is 4 byte

Related Topics

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.