Data Types in C Language | Types of Data Types in C
Today we are going to learn about what is data types in C language? And what are the types of Data Types in C.
But before proceeding, if you don’t know about the compiler and the variable, then first you should learn about it. because it will help you to understand data types better.
You read about it from here -: What is a variable And what is a compiler
So let’s understand what is Data Types in C Language
What is Data Types in C Language?
In C language, Whenever we create a variable to store any data or information, then while declaring that variable, we also need to declare which type of data that variable is going to store.
These data types can be int, char, float, double, and by looking at these data types, it is known which type of value is going to be stored in the variable.
We store Integer value in variables, created with int data type, and in variables created with char data type we store character type of data, and in variables created by float data type we store floating point values.
When declaring a variable, we declare its type because when the compiler converts our source code into machine code then the compiler will allocate some memory in ram for that variable according to that data type.
So we declare the data type of variable to tell the compiler, which type of data is going to be stored in that variable. so that the compiler allocates memory in ram for that variable correctly according to data types.
When declaring a variable, its type is also declared. so that the compiler allocates as much memory to the variable as it needs. If we don’t do this then memory loss may be more.
In easy words, Data Types tells what kind of data we are going to store in a variable.
Let’s understand data types with an example -:
Example of Data Types in C
// Example of Data Types in C
#include <stdio.h>
int main ()
{
int x = 14;
char ch = 'a';
float f1 = 25.7;
printf ("x = %d, ch = %c, f1 = %f", x, ch, f1);
return (0);
}
Output -:
x = 14, ch = a, f1 = 25.700001
In this example, we have done a variable declaration by doing int x, in which int is a data type and x is the name of variable.
By looking int data type, it is clear to know that we will store an integer type value in the variable x.
If we have to store a character type data in our program, then for that we have to use a char data type. like we have done a variable declaration by char ch, in this program and store the character ‘a’.
Similarly, to store the floating point data in our program, we used a float data type with a variable named f1 and stored 25.7 data.
There may be a need to store many types of data in a program. According to the kind of data we have to store, we can use many types of data types in C language.
Now let’s learn how many types of data types in C language.
Types of Data Types in C Language
There are mainly three types of data types in C language -:
- Pre-defined data types
- Derived data types
- User-defined data type
1. Pre-defined Data Types
Such data types are called Pre-defined Data Types which are already defined and don’t need to be defined separately, such as int, char, float, double, void, etc. These data types come in Basic Data Types.
These data types, don’t mean to tell the compiler separately because they are already defined. And the data types that are also keywords is called primitive data types.
List of Predefined Data Types
Pre-defined Data Types | Examples |
Integer Type | int, long int, short int, unsigned int |
Character Type | char |
Floating Point Data Type | Float , double |
Integer Type
- The “int” keyword is used to create a variable of integer type.
- We store numeric values in the variable of Integer Type.
- Variables that are created of “int” data type can store 2 byte, 4 byte, and 8-byte data. But this thing depends on the compiler.
- Some compilers store 2-byte data in a variable made of int data type, while some store 4-byte data.
- int (2 byte) can store values from -32,768 to +32,767.
- int (4 byte) can store values from -2,147,483,648 to + 2,147,483,647.
- If you want to store more data value in variables then you can create a variable using the keyword “long int”. With the help of this keyword, the variable can store more amount of data than the normal variable.
- The ” %d ” Format Specifier is used to print the value of a variable made of int data type.
Note
- We don’t store decimal values in variables created with the help of int data type.
- If we store the decimal value in a variable created with int data type, then the decimal points of that value will be cut and only the whole number will be stored.
- If we want to store the decimal value then we have to use float and double data type.
Syntax -:
int x = 5;
long int y;
Example
#include <stdio.h>
int main ()
{
int x = 5;
long int y;
printf ("Enter a Number \n");
scanf ("%d", &y);
printf ("value of x = %d, y = %d", x, y);
return 0;
}
Output -:
Enter a Number
10
value of x = 5, y = 10
Character Type
- We store character type data in a variable made with char data type.
- The “char” keyword is used to create a variable of character type.
- A variable created by the keyword “char” stores a single character.
- Variables made with Character Type have a size of 1 byte.
- The %c Format Specifier is used to print the value of a variable made of char data type.
Syntax
char ch = 'A';
char ch2 = 'a';
Example
#include <stdio.h>
int main ()
{
char ch = 'A';
char ch2;
printf ("Enter a Character \n");
scanf ("%c", &ch2);
printf ("value of ch = %c, ch2 = %c", ch, ch2);
return 0;
}
Output -:
Enter a Character
B
value of ch = A, ch2 = B
Floating Point Data Type
Floating point data type consists of two types -:
- Float
- Double
Float
- It is used to store decimal points values.
- The “float” keyword is used to create a variable of floating point type.
- The variable made from Float Type has a size of 4 bytes.
- The %f Format Specifier is used to print the value of a variable made of Float data type.
Example of Float Data Type
#include <stdio.h>
int main ()
{
float x = 5.50;
float y;
printf ("Enter a decimal Number \n");
scanf ("%f", & y);
printf ("sum of x and y is %f", x + y);
return 0;
}
Output -:
Enter a decimal number
10.06
Sum of x and y is 15.560000
Double
- It is also used to store data with decimal points like float data type.
- The “double” keyword is used to create a variable of double data type.
- A variable made of double type has a size of 8 byte.
- The %lf Format Specifier is used to print the value of a variable made of double data type.
Example of Double Data Type
#include <stdio.h>
int main ()
{
float x = 679999999.454;
double y = 679999999.454;
printf ("float x = %f and double y = %lf", x, y);
return 0;
}
Output
float x = 680000000.000000 and double y = 679999999.454000
Friends, all the data types that you have known about above are called basic data types or pre-defined data types. Let’s now learn about Derived Data Types.
2. Derived Data Types
Derived Data Types are data types that contain grouping of variables.
Derived Data Types is as follows:
We will learn about these Derived Data Types in our further post details.
3. User-Defined Data Types
Data Types that are not already defined and which have to be defined separately are called User-Defined Data Types. Such as – Structure, Enumerator, union.
Read More -:
- Variables in C
- Constants liters in C
- Comments in C
- Format specifier in C
- Escape Sequence in C
- Type Casting in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I have tried to give full information about what data types do you have in C language? And how many types of data types.
I hope you have found the answer to your question and you will not have to search anywhere else about what is Data Types in C.
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the data types in C language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.