A data type specifies the type of data that a variable can store for example integer, floating, character, etc.
Each variable in C has an associated data type. It determines the type and size of data associated with variables.
As the name suggests, a Datatype defines the type of data being used. Each data type requires different amounts of memory and has some specific operations which can be performed over it.
Different categories of data types in the C language
Type | Example |
---|---|
Basic | character, integer, floating-point, double. (int, char, float, double) |
Derived | array, pointer, structure, union |
Enumeration | enums |
Bool type | true or false |
void | Empty value (void) |
Basic Data Types
Following are the examples of some very common data types used in C.
- char
- It stores a single character and requires a single byte of memory in almost all compilers.
- int
- An int variable is used to store an integer.
- Integers are whole numbers that can have both zero, positive and negative values but no decimal values.
- For example,
0
,-5
,10
- float: It is used to store decimal numbers (numbers with floating point value) with single precision.
- double: It is used to store decimal numbers with double precision.
- void: It means “nothing” or “no type”.
Data Types | Memory Size | Range |
---|---|---|
char | 1 byte | −128 to 127 |
signed char | 1 byte | −128 to 127 |
unsigned char | 1 byte | 0 to 255 |
short | 2 byte | −32,768 to 32,767 |
signed short | 2 byte | −32,768 to 32,767 |
unsigned short | 2 byte | 0 to 65,535 |
int | 2 byte | −32,768 to 32,767 |
signed int | 2 byte | −32,768 to 32,767 |
unsigned int | 2 byte | 0 to 65,535 |
short int | 2 byte | −32,768 to 32,767 |
signed short int | 2 byte | −32,768 to 32,767 |
unsigned short int | 2 byte | 0 to 65,535 |
long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
signed long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 byte | 0 to 4,294,967,295 |
float | 4 byte | |
double | 8 byte | |
long double | 10 byte |
We will learn about all data types in later tutorials.
Example
#include >stdio.h>
int main() {
// datatypes
int a = 10;
char b = 'S';
float c = 2.88;
double d = 28.888;
printf("Integer datatype : %d\n",a);
printf("Character datatype : %c\n",b);
printf("Float datatype : %f\n",c);
printf("Double Float datatype : %lf\n",d);
return 0;
}