On this page, you will learn about data types in C programming, What are the primary data types in C? What are the format specifiers? How to find the size of int, float, double, and char in a C program What is a derived data type in C programming? What is the enumeration data type in C? And what is the void data type in C?
Data type is a classification of the type of data that a variable or expression can hold in a programming language. In C programming, there are several data types that can be used to store different kinds of data such as integers, floating-point numbers, characters, and arrays.
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 |
In C programming language, a data type is a classification of various types of data, each with its own set of values and operations that can be performed on it. The primary data types in C are:
int
data type can store both positive and negative whole numbers. For example, int x = 10;
declares a variable x
of type int
with a value of 10.float y = 3.14;
declares a variable y
of type float
with a value of 3.14.float
data type. For example, double z = 3.14159265359;
declares a variable z
of type double
with a value of 3.14159265359.char ch = 'a';
declares a variable ch
of type char
with a value of 'a'.void func();
declares a function func()
that does not return any value.These primary data types in C can be used to declare variables and define functions, and are the building blocks of more complex data structures and types.
Format specifiers are special characters used in C programming language to represent a variable when it is printed or scanned using the printf() and scanf() functions, respectively. They specify the type of the data being printed or scanned.
Data Types | Memory Size | Format Specifiers | Range |
---|---|---|---|
char | 1 byte | %c | −128 to 127 |
signed char | 1 byte | %c | −128 to 127 |
unsigned char | 1 byte | %c | 0 to 255 |
short | 2 bytes | %h | −32,768 to 32,767 |
signed short | 2 bytes | %hd | −32,768 to 32,767 |
unsigned short | 2 bytes | %hu | 0 to 65,535 |
int | 2 bytes | %d or %i | 0 to 65,535−32,768 to 32,767 |
signed int | 2 bytes | %hd | −32,768 to 32,767 |
unsigned int | 2 bytes | %hu | 0 to 65,535 |
short int | 2 bytes | %d or %i | −32,768 to 32,767 |
signed short int | 2 byte | %hd or %hi | −32,768 to 32,767 |
unsigned short int | 2 bytes | %hu | 0 to 65,535 |
long int | 4 bytes | %ld | -2,147,483,648 to 2,147,483,647 |
signed long int | 4 byte | %ld | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 bytes | %lu | 0 to 4,294,967,295 |
float | 4 bytes | %f | 3.4E-38 to 3.4E+38 |
double | 8 bytes | %lf | 1.7E-308 to 1.7E+308 |
long double | 10 bytes | %Lf | 3.4E-4932 to 1.1E+4932 |
Different data types have different ranges,format specifiers and how much memory it takes to store.This ranges may be different
from compiler to compiler. We can use the sizeof()
operator to check the size of a variable. See the following C program to check out which data types take how much memory:
#include<stdio.h>
int main() {
printf("int takes: %lu bytes.\n",sizeof(int)); // '\n' is for goto new line
printf("float takes: %lu bytes.\n",sizeof(float));
printf("double takes: %lu bytes.\n",sizeof(double));
printf("char take: %lu bytes.\n",sizeof(char));
return 0;
}
int takes: 4 bytes.
float takes: 4 bytes.
double takes: 8 bytes.
char take: 1 bytes.
Derived data types are data types that are derived from the primary data types in C programming language. They are created by using the primary data types in combination with different operators and modifiers.
There are several examples of derived data types in C programming language, including:
Derived data types are an essential part of C programming language, and understanding them is crucial for writing efficient and effective code. An example given below:
#include<stdio.h>
struct Person {// structure data
char name[20];
int age;
float height;
};
union Number { // union data
int workerNo;
}j;
int main() {
int num[5] = {1,2,3,4,5}; // array data
int test = 100;
int *p = &test;// pointer data
struct Person p1;
p1.age = 20;
p1.height = 5.8;
j.workerNo = 100;
printf("The array data num[3] = %d\n",num[3]);
printf("The pointer data *p = %d\n",*p);
printf("The struct data p1.age = %d\n",p1.age);
printf("The union data job.workerNo = %d\n",j.workerNo);
return 0;
}
The array data num[3] = 4
The pointer data *p = 100
The struct data p1.age = 20
The union data job.workerNo = 100
Enumeration Data Type is a user-defined data type that consists of a set of named values, also known as enumerators. Each enumerator is assigned an integer value, which starts from 0 by default and increments by 1 for each successive enumerator. Enumeration Data Type is useful for creating a set of related named values and making the code more readable and understandable.
Here's an example code for Enumeration Data Type in C:
#include<stdio.h>
enum Weekdays {
Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday,
Sunday
};
int main() {
enum Weekdays today = Wednesday;
printf("Today is %d\n", today);
return 0;
}
Today is 2
In C programming language, void is a data type used to indicate the absence of a specific type. It means that a function will not return any value, or a pointer variable will not point to any data type.
Void can also be used as a parameter in a function, indicating that the function does not take any parameters. Here's an example:
#include<stdio.h>
void printNumber(int num) {
printf("The number is: %d\n", num);
}
int main() {
printNumber(5);
return 0;
}
The number is: 5