A data type is a type associated with an entity. Data Types specify which type of data will be stored in a variable, constant, etc. Data Types are classified mainly into 2 types But a void data type also exists that is ignored by every beginner. Other websites and books can classify data types differently. But it is an easy way to classify and understand for beginners.
Primary Data Types
Primary Data Types can be further classified into integers, Floating Numbers, characters, etc
- Integer like 1,2,-2,-4,etc
- Floating Numbers like 1.2,2.4,-3.5,etc
- Characters like a,c,d,j, etc
Secondary Data Types
Array, Structure, Union, and Enum come under secondary data types.
Secondary Data Types are mainly used to store the value of more than one primary data type at once.
Let’s talk about all data types one by one using examples.
Integer Data Types
An integer is a data type used to store a whole number without point values. int keyword is reserved for Integer data type. %d is the format specifier for the Integer data type. Its size can be 2 bytes or 4 bytes. Example of Integer Data Type:
#include<stdio.h>
#include<conio.h>
void main()
{
int a; // Decleration of Integer Data Type
printf("Enter a Integer\n");
scanf("%d",&a); // Getting value of Integer from user
printf("Value of Integer=%d", a); // Printing value of Integer
getch();
}
Float Data Types
Float is a data type that store a real number. float is a reserved keyword used for Floating Numbers. %f is the format specifier for the FLoat Data type. Its size will be always double of an integer. Mostly a float number contains 4 bits. Example of FLoat Data Type:
#include<stdio.h>
#include<conio.h>
void main()
{
float a; // Decleration of Float Data Type
printf("Enter a Float\n");
scanf("%f",&a); // Getting value of Float from user
printf("Value of FLoat=%f", a); // Printing value of Float
getch();
}
Character Data Types
Character data type allows the user to store a single character value. char is the reserved word in C for the character data type. %c is the format identifier for the character data type. The single character size is 1 byte. Example of character data type:
#include<stdio.h>
#include<conio.h>
void main()
{
char a; // Decleration of Char Data Type
printf("Enter a Character\n");
scanf("%c",&a); // Getting value of Char from user
printf("Value of Character=%c",a); // Printing value of Char
getch();
}
Array Data Types
The array is a data type used to store multiple values of a single variable. We can store an n number of Integers or Characters or Float Number at once. %d, %f, and %c are the Format Specifier of an array. Example of an Array:
#include<stdio.h>
#include<conio.h>
void main()
{
// Array Contains Integer Value
int arr_int[5]={1,3,2,5,7};
// Array Contains Float Value
float arr_float[5]={1.3,3.5,5.7,7.8,9.2};
// Set of Characters is known as String.
// Array Contains a String
char arr_char[5]={"SHARE"};
// Let's Print first value of all three arrays
printf("%d\n, %f\n ,%c\n",arr_int[0], arr_float[0], arr_char[0]);
// Changing an array element
arr_int[0]=4;
printf("\n %d",arr_int[0]);
// Print Complete Array using Loop
int i;
for(i=0;i<5;i++)
{
printf("%d ",arr_int[i]);
}
for(i=0;i<5;i++)
{
printf("%f ",arr_float[i]);
}
for(i=0;i<5;i++)
{
printf("%c ",arr_char[i]);
}
getch();
}
Structure Data Types
The structure is similar to an array used to store many variables at once. But there is a huge difference that is Structure stores every type of variable but array stores only similar types of variables. Let’s take a look at the example for a better understanding.
#include<stdio.h>
#include<conio.h>
// Declaration of Structure
struct STUDENT{
char Name[50];
int roll_no;
float Percentage;
char Department[50];
};
int main()
{
// Entering Values in STUDENT Structure
struct STUDENT s1={"RAHUL", 14, 92.5, "CS"};
// Printing values of Structure
printf("%s, %d, %f, %s\n", s1.Name,s1.roll_no, s1.Percentage, s1.Department);
// Taking Input from User in STUDENT Structure
struct STUDENT s2;
scanf("%s %d %f %s",s2.Name ,&s2.roll_no, &s2.Percentage, s2.Department);
printf("%s, %d, %f, %s\n", s2.Name,s2.roll_no, s2.Percentage, s2.Department);
return 0;
}
Union Data Types
Union is very similar to structure. In a union, all members share the same memory location but the structure does not. There will be a similar syntax as structure. So, I will not explain it much more. Enum Data type is not used at the beginner level so it is not much important for it now. If you want about Union and Enum then you can read C programming official documents.
I hope you all understood the basic instructions for Data Types and how to use them. Check out other amazing articles given below.