In the C programming language, data types define the type of data that can be stored in variables. Each data type has a specific range of values and occupies a certain amount of memory. Understanding data types is crucial for effectively working with variables and performing operations on them. In this tutorial, we'll explore the basic data types in C and their characteristics, presented in a table format.
There are four types of data types in C language:
Types | Data Types |
---|---|
Basic Data Type | int, char, float, double |
Derived Data Type | array, pointer, structure, union |
Enumeration Data Type | enum |
Void Data Type | void |
Data Type | Memory Size (in bytes) | Range (signed) | Range (unsigned) |
---|---|---|---|
char | 1 | -128 to 127 | 0 to 255 |
int | 2 or 4 | -32,768 to 32,767 (2 bytes) | 0 to 65,535 (2 bytes) or -2,147,483,648 to 2,147,483,647 (4 bytes) |
short | 2 | -32,768 to 32,767 | 0 to 65,535 |
long | 4 or 8 | -2,147,483,648 to 2,147,483,647 (4 bytes) | 0 to 4,294,967,295 (4 bytes) or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (8 bytes) |
float | 4 | N/A | N/A |
double | 8 | N/A | N/A |
Category | Data Type | Memory Size (in bytes) | Value Range |
---|---|---|---|
Integer | char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 | |
short | 2 | -32,768 to 32,767 | |
unsigned short | 2 | 0 to 65,535 | |
int | 2 or 4 | -32,768 to 32,767 (2 bytes) or | |
-2,147,483,648 to 2,147,483,647 | |||
(4 bytes) | |||
unsigned int | 2 or 4 | 0 to 65,535 (2 bytes) or | |
0 to 4,294,967,295 (4 bytes) | |||
long | 4 or 8 | -2,147,483,648 to 2,147,483,647 | |
(4 bytes) or | |||
-9,223,372,036,854,775,808 to | |||
9,223,372,036,854,775,807 (8 | |||
bytes) | |||
unsigned long | 4 or 8 | 0 to 4,294,967,295 (4 bytes) or | |
0 to 18,446,744,073,709,551,615 | |||
(8 bytes) | |||
Floating | float | 4 | 1.2E-38 to 3.4E+38 |
Point | double | 8 | 2.3E-308 to 1.7E+308 |
long double | 10, 12, or 16 | 3.4E-4932 to 1.1E+4932 | |
Character | char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 | |
wchar_t | 2 or 4 | 0 to 65,535 (2 bytes) or | |
0 to 4,294,967,295 (4 bytes) | |||
char16_t | 2 | 0 to 65,535 | |
char32_t | 4 | 0 to 4,294,967,295 | |
_Bool | 1 | 0 or 1 |
These are some of the commonly used data types in C. Each data type has its own memory size and range of values, which should be considered when selecting the appropriate type for your variables.
The char data type is used to store individual characters and occupies 1 byte of memory. It can represent both positive and negative values.
char Data Type Example:
The int data type is used for integers and can have different memory sizes depending on the platform (2 bytes or 4 bytes). It can represent both positive and negative values.
int Data Type Example:
The short data type is used for short integers and occupies 2 bytes of memory. It can represent both positive and negative values.
int Data Type Example:
The long data type is used for long integers and can have different memory sizes depending on the platform (4 bytes or 8 bytes). It can represent both positive and negative values.
long Data Type Example:
The float data type is used for single-precision floating-point numbers and occupies 4 bytes of memory. It is capable of representing fractional values.
float Data Type Example:
The double data type is used for double-precision floating-point numbers and occupies 8 bytes of memory. It provides higher precision compared to the float data type.
double Data Type Example:
It's important to note that the ranges mentioned in the table above are for signed data types. When using unsigned data types, the entire range becomes positive values starting from zero.
By understanding the characteristics of different data types, you can choose the most suitable type for your variables, ensuring efficient memory usage and appropriate value representation in your programs.
Understanding the data types in C and their properties is essential for effectively working with variables and performing operations on them. By selecting the appropriate data type, you can ensure accurate storage and manipulation of data in your C programs.