Data Types in C programming
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?
What is data type and what are the data types in c programming?
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 |
What are the Primary Data Types in C?
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: This data type is used to represent integer values. The
int
data type can store both positive and negative whole numbers. For example,int x = 10;
declares a variablex
of typeint
with a value of 10. - float: This data type is used to represent floating-point numbers. It is used to store decimal values. For example,
float y = 3.14;
declares a variabley
of typefloat
with a value of 3.14. - double: This data type is used to represent double precision floating-point numbers. It can store larger decimal values with greater precision than the
float
data type. For example,double z = 3.14159265359;
declares a variablez
of typedouble
with a value of 3.14159265359. - char: This data type is used to represent single characters. It can store any single character, including letters, digits, and special characters. For example,
char ch = 'a';
declares a variablech
of typechar
with a value of 'a'. - void: This data type is used to represent the absence of a data type. It is commonly used as a return type for functions that do not return a value. For example,
void func();
declares a functionfunc()
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.
What are format specifiers?
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:
How to find the size of int, float, double and char in c program:
#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;
}
Output:
int takes: 4 bytes.
float takes: 4 bytes.
double takes: 8 bytes.
char take: 1 bytes.
What is a derived data type in c programming?
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:
- Arrays: Arrays are collections of similar data elements. They are derived from the primary data types and are created by grouping variables of the same data type.
- Pointers: Pointers are variables that store the memory address of another variable. They are derived from the primary data types and are created by using the address-of operator (&) and the indirection operator (*).
- Structures: Structures are user-defined data types that allow you to combine variables of different data types into a single unit. They are derived from the primary data types and are created by using the struct keyword.
- Union: Union is similar to structures, but it allows you to store different data types in the same memory location. Union is created by using the union keyword.
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>
int main() {
int num[5]; // array data
int *p; // pointer data
struct Person {// structure data
char name[20];
int age;
float height;
};
union Number { // union data
int i;
float f;
};
return 0;
}
What is the enumeration data type in c?
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;
}
What is the void data type in c?
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;
}