Int Data Type in C: Usage and Examples
☰Fullscreen
Table of Content:
- Integer data type allows a variable to store numeric values.
intkeyword is used to refer integer data type.- The storage size of int data type is 2 or 4 or 8 byte.
- It varies depending upon the processor in the CPU that we use. If we are using 16-bit processor, 2 bytes (16 bit) of memory will be allocated for int data type.
Likewise, 4 bytes (32 bit) of memory for 32-bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
int (2 byte) can store values from -32,768 to +32,767
- int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
- If you want to use the integer value that crosses the above limit, you can go for
long intandlong long intfor which the limits are very high.
Points To Be Remember:
- We can't store decimal values using int data type.
- If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
- In this case, float data type can be used to store decimal values in a variable.
-
Example:
int a = 100000, int b = -200000
| Data Type | Size(Byte) | Range |
|---|---|---|
| int | 2 or 4 | 32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
int Variable Declaration and Variable Initialization:
Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.
byte age;
Variable Initialization : To initialize a variable you must assign it a valid value.
age= 2 ;
int Variable Declaration and Variable Initialization in two steps:
byte age; age = 20;
Program
#include void main() { int age; age = 20; printf("%d \n",age); }
Output
When you compile and execute the above program, it produces the following result
20 Press any key to continue . . .
int Variable Declaration and Variable Initialization in one line:
int age = 20;
Program
#include void main() { int age = 20; printf("%d \n",age); }
Output
When you compile and execute the above program, it produces the following result
20 Press any key to continue . . .
Another Example:
Program
#include void main() { int number1 = 22; int number2 = 23; int c; c = number1 + number2 ; printf("Sum of above two Number : %d \n",c); }
Output
When you compile and execute the above program, it produces the following result
Sum of above two Number : 45 Press any key to continue . . .
- Question 1: Can I use "int" data type to store the value 32768? Why?
Related Questions
- Assignment 1: Write a program to illustrate the results of type conversion between signed and unsigned long and short integer data types.