×

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

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

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

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

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.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.