What is the difference between an int and a long in C++?


int

The datatype int is used to store the integer values. It could be signed or unsigned. The datatype int is of 32-bit or 4 bytes. It requires less memory area than long to store a value. The keyword “int” is used to declare an integer variable.

The following is the syntax of int datatype.

int variable_name;

Here,

variable_name − The name of variable given by user.

The following is an example of int datatype.

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   int a = 8;
   int b = 10;
   int c = a+b;
   cout << "The value of c : " << c;
   return 0;
}

Output

The value of c : 18

long

The datatype long is used to store the long integer values. It could be signed or unsigned. The datatype long is of 64-bit or 8 bytes. It requires more memory area than int to store the value. The keyword “long” is used to declare a long integer variable.

The following is the syntax of long datatype.

long variable_name;

Here,

variable_name − The name of variable given by user.

The following is an example of long datatype.

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   int a = 8;
   long b = 28;
   long c = long(a+b);
   cout << "The value of c : " << c;
   return 0;
}

Output

The value of c : 36

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

996 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements