Primitive Data Types in C
Introduction
Data types in C are used to represent the type of data that variables can store. C language supports several types of data, but they can broadly be classified into two categories: primitive data types and derived data types. In this tutorial, we will focus on primitive data types.
Primitive Data Types
Primitive data types are the most basic data types available in C. They are predefined in the C language and are used to represent simple values. The primary primitive data types in C are:
intfloatdoublechar
int
The int type is used to store integer values. An integer is a whole number without a decimal point.
Example:
int main() {
int a = 10;
int b = 20;
int sum = a + b;
printf("Sum: %d", sum);
return 0;
}
float
The float type is used to store floating-point numbers, which are numbers that have a decimal point.
Example:
int main() {
float a = 5.5;
float b = 2.2;
float sum = a + b;
printf("Sum: %.2f", sum);
return 0;
}
double
The double type is similar to float but with double the precision. It is used to store large floating-point numbers.
Example:
int main() {
double a = 10.123456;
double b = 20.654321;
double sum = a + b;
printf("Sum: %.6lf", sum);
return 0;
}
char
The char type is used to store single characters. It can hold any character from the ASCII character set.
Example:
int main() {
char a = 'A';
char b = 'B';
printf("Characters: %c %c", a, b);
return 0;
}
Summary
Primitive data types are the building blocks for data manipulation in C. They include int for integers, float for floating-point numbers, double for double-precision floating-point numbers, and char for characters. Understanding these types is fundamental to programming in C.
