While writing a program in any programming language, we use variables to store information of different types. The information we store can be a number, string, etc. Variables are used to reserve memory locations to store values.
While declaring a variable, we need to specify its data type to restrict the type of data to be stored. In other words, the data type dictates the variable what type of data it can store. Every data type needs some amount of memory which varies from one data type to another. Each data type has some specific operations which can be performed over it.
In any programming language, the data type is defined as a collection of data with values having a specific fixed meaning and characteristics. Integer, character, etc are some of the most common data types. A programming language specifies the range for a data type. Data types are used in order to identify the: type of a variable when declared, the return type of function, and what type of parameters are expected by the function.
Types of Data types
Primitive Data Types
Int
This data type is used in order to store integer values.
Memory used by int data type is 4 bytes.
The range of int data type is from -2,147,483,648 to 2,147,483,647
Char
This data type is used in order to store one single character, and it requires only a single byte of memory.
Memory used by char data type is 1 byte
The range of char data type is from -128 to 127
Float
This data type is used in order to store decimal numbers with single precision.
Memory used by float data type is 4 bytes.
The range of float data type is from 1.2E-38 to 3.4E+38
Double
This data type is used in order to store decimal numbers with double precision.
Memory used by double data type is 8 bytes.
The range of double data type is from 2.3E-308 to 1.7E+308.
Void
This data type is used with function when the function doesn't return any value.
Example
#include <stdio.h>
int main()
{
int x = 87;
char y = 'Y';
double z = 3.14;
float a=3.14;
printf("Char value is %c \n",y);
printf("Int value is %d \n",x);
printf("Double value is %lf \n",z);
printf("Float value is %.3f \n",a);
}
Output
Char value is Y
Int value is 87
Double value is 3.140000
Float value is 3.140
An array is defined as the collection of similar items stored at continuous memory locations, an array is basically a sequence of data items that have homogeneous values. The idea of using an array is to represent many instances in one variable.
Syntax
DataType Array_Name( size_of_array );
Example
#include <stdio.h>
int main()
{
int arr[5];
for(int i=0;i<5;i++){
arr[i]=i+1;
}
for(int i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}
Pointers refer to a variable that holds the address of another variable; like any other variable, they too have a data type. It allows programs to simulate call-by-reference as well as create and manipulate dynamic Data Structures.
Syntax
DataType *variable_name;
Example
#include <stdio.h>
int main()
{
// a variable
int x = 10;
// A pointer variable
int *ptr_x = &x;
// This line prints value at address stored in ptr_x.
// Value stored is value of variable "x"
printf("Value of Variable x = %d\n", *ptr_x);
// This gives the address of the pointer variable
printf("Address of Pointer Variable = %p\n", ptr_x);
// ptr_x can be used to manipulate the value as well
*ptr_x = 20; // Value at address is now 20
// This prints 20
printf("After doing *ptr_x = 20, *ptr_x is %d\n", *ptr_x);
return 0;
}
A reference variable is an alias, i.e., another name given to a variable that has already been declared. A reference variable gets attached to the memory location of the original variable, thereby allowing the user to access the contents of the variable by either the original variable name or the reference.
A structure is used to create a data type which is a collection of variables of different data types grouped together under a single name. ‘struct’ keyword is used in order to declare a structure in the C programming language.
Syntax
struct Structure_Name{
// variables of different data types
};
Example
#include <stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p;
p.x=5; p.y=2;
printf("Area of the Rectangle is %d ",p.x*p.y);
}
In union, all the members which are defined share the same memory location, i.e., any change brought in one member of the union will be reflected in all other members.
Syntax
union Union_name{
// data members
};
Example
#include <stdio.h>
union point {
int x, y;
};
int main()
{
// A union variable p
union point p;
p.x = 30;
printf("After making x = 2:\n x = %d, y = %d\n\n", p.x, p.y);
// if we alter value of one it will affect the other
p.y = 40;
printf("After making y = 10:\n x = %d, y = %d\n\n", p.x, p.y);
return 0;
}
It is a user-defined data type whose main purpose is to make the program easy to read and maintain. In short, it is used to assign meaning full names to integral constants. The keyword ‘enum’ is used to define an enumeration in C language.
What is the difference between a structure and a union?
The difference between structure and union is that the size of the structure is greater than or equal to the size of its member, whereas the size of the union is equal to the size of the largest member.
Why are pointers used?
Pointers are used in order to store and manage the addresses of dynamically allocated blocks of memory, which are used to store data objects or arrays of objects.
What is the difference between float and double?
Double has twice the precision as compared to float. Float takes 4 bytes of storage, whereas double takes 8 bytes.
What are the four main data types?
The four main data types in C++ are int (for integers), float (for floating-point numbers), char (for single characters), and bool (for Boolean values). These data types serve as the building blocks for creating variables and managing data in C++.
Conclusion
In this article, we have explored the significance of data types in C. We began by discussing different data types for effective programming. Next, we looked at various types of data types available in C, including integers, floats, characters, and booleans.