Integer Data Type
The integer datatype in C is used to store the integer numbers(any number including positive, negative and zero without decimal part). Octal values, hexadecimal values, and decimal values can be stored in int data type in C.
- Range: -2,147,483,648 to 2,147,483,647
- Size: 4 bytes
- Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:
int var_name;
The integer data type can also be used as
- unsigned int: Unsigned int data type in C is used to store the data values from zero to positive numbers but it can’t store negative values like signed int.
- short int: It is lesser in size than the int by 2 bytes so can only store values from -32,768 to 32,767.
- long int: Larger version of the int datatype so can store values greater than int.
- unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler-dependent. We can use sizeof operator to check the actual size of any data type.
Example of int
C
// C program to print Integer data types. #include <stdio.h> int main() { // Integer value with positive data. int a = 9; // integer value with negative data. int b = -9; // U or u is Used for Unsigned int in C. int c = 89U; // L or l is used for long int in C. long int d = 99998L; printf ( "Integer value with positive data: %d\n" , a); printf ( "Integer value with negative data: %d\n" , b); printf ( "Integer value with an unsigned int data: %u\n" , c); printf ( "Integer value with an long int data: %ld" , d); return 0; } |
Integer value with positive data: 9 Integer value with negative data: -9 Integer value with an unsigned int data: 89 Integer value with an long int data: 99998
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning as well as its characteristics.
The data types in C can be classified as follows:
Types |
Description |
---|---|
Primitive Data Types | Primitive data types are the most basic data types that are used for representing simple values such as integers, float, characters, etc. |
User Defined Data Types | The user-defined data types are defined by the user himself. |
Derived Types | The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. |
Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is a list of ranges along with the memory requirement and format specifiers on the 32-bit GCC compiler.
Data Type |
Size (bytes) |
Range |
Format Specifier |
---|---|---|---|
short int |
2 |
-32,768 to 32,767 |
%hd |
unsigned short int |
2 |
0 to 65,535 |
%hu |
unsigned int |
4 |
0 to 4,294,967,295 |
%u |
int |
4 |
-2,147,483,648 to 2,147,483,647 |
%d |
long int |
4 |
-2,147,483,648 to 2,147,483,647 |
%ld |
unsigned long int |
4 |
0 to 4,294,967,295 |
%lu |
long long int |
8 |
-(2^63) to (2^63)-1 |
%lld |
unsigned long long int |
8 |
0 to 18,446,744,073,709,551,615 |
%llu |
signed char |
1 |
-128 to 127 |
%c |
unsigned char |
1 |
0 to 255 |
%c |
float |
4 |
1.2E-38 to 3.4E+38 | %f |
double |
8 |
1.7E-308 to 1.7E+308 | %lf |
long double |
16 |
3.4E-4932 to 1.1E+4932 | %Lf |
Note: The long, short, signed and unsigned are datatype modifier that can be used with some primitive data types to change the size or length of the datatype.
The following are some main primitive data types in C: