Data types in C language

In the C programming language, data types are used to define the kind of data that a variable can store. The data types in C include integer, character, float, double, and void, among others. Each data type has a unique format specifier, storage size, and value range.

Data Type
Format Specifier
Size (in bytes)
char
%c
1
short
%hd
2
int
%d
4
long
%ld
8
float
%f
4
double
%lf
8
long double
%LF
16
unsigned char
%c
1
unsigned short
%hu
2
unsigned int
%u
4
unsigned long
%lu
8

Integer Data Type:

The integer data type is used to store whole numbers. It can be classified into three types: char, int, and long.

a) char: It is used to store a single character, which can be a letter, digit, or special character. The size of char data type is 1 byte.

Example:

char grade = 'A';

b) int: It is used to store integers with a size of 2 or 4 bytes. The range of int data type is -32,768 to 32,767 for 2 bytes and -2,147,483,648 to 2,147,483,647 for 4 bytes.

Example:

int age = 25;

c) long: It is used to store large integers with a size of 4 or 8 bytes. The range of long data type is -2,147,483,648 to 2,147,483,647 for 4 bytes and -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for 8 bytes.

Example:

long salary = 50000;

Floating-Point Data Type:

The floating-point data type is used to store decimal numbers. It can be classified into two types: float and double.

a) float: It is used to store floating-point numbers with a size of 4 bytes. The range of float data type is approximately ±3.4E±38 and it has 7 decimal digits of precision.

Example:

float salary = 50000.50;

b) double: It is used to store floating-point numbers with a size of 8 bytes. The range of double data type is approximately ±1.7E±308 and it has 15 decimal digits of precision.

Example:

double weight = 65.5;

Void Data Type:

The void data type is used to represent the absence of the type. It is commonly used to indicate that a function returns nothing.

Example:

void printMessage() {
   printf("Hello World!");
}

Output:

Hello World

Boolean Data Type:

The boolean data type is used to store only two values, which are true or false. In C language, there is no boolean data type, so we use int instead. 0 represents false and any non-zero value represents true.

Example:

int isPassed = 1;

In conclusion, data types are crucial to the C language because they define the kinds of information that a variable can store. We can conserve memory and guarantee that the data is appropriately stored and retrieved by selecting the right data type.