Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

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.

Also See, Sum of Digits in C, C Static Function

What is Data type?

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 


Also see, Floyd's Triangle in C

Derived Data types

Array 

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;
}

 

Output

1 2 3 4 5 

 

Pointers

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;
}

 

Output

Value of Variable x = 10
Address of Pointer Variable = 0x7ff7b6846418
After doing *ptr_x = 20, *ptr_x is 20

Also read - Bit stuffing program in c

References 

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.

Syntax

DataType& reference_variable_name = original_variable ;

User Defined Data types

Structure

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);
}

 

Output

Area of the Rectangle is 10 

 

Union

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;
}

 

Output

After making x = 2:
x = 30, y = 30

After making y = 10:
x = 40, y = 40


Also see, Tribonacci Series and Short int in C Programming

Enumeration 

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. 

Syntax

enum flag{constant1, constant2, constant3, ....... };

 

Example

#include<stdio.h>

enum day{Mon,Tue,Wed,Thurs,Fri,Sat,Sun};

int main()
{
    int i;
    for (i=Mon; i<=Sun; i++)    
        {printf("%d ", i);}
            
    return 0;
}

 

Output 

0 1 2 3 4 5 6 


Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

Ques: What is the difference between a structure and a union?

Ans: 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.
 

Ques: Why are pointers used?

Ans: 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.

 

Ques: What is the difference between float and double?

Ans: Double has twice the precision as compared to float. Float takes 4 bytes of storage, whereas double takes 8 bytes.

Key Takeaways:

In this blog, we have covered the following things:

  • First, we discussed why we need data types.
  • Then we discussed the various types of data types available in C.
     

To study more about data types, refer to Abstract Data Types in C++.

To understand the difference between classes and structure, refer to this.

The values stored in variables of different data types can be converted into others; to know more about it, refer to this.

Topics covered
1.
Introduction
2.
What is Data type?
3.
Types of Data types
4.
Primitive Data Types
4.1.
Int  
4.2.
Char
4.3.
Float 
4.4.
Double
4.5.
Void 
5.
Derived Data types
5.1.
Array 
5.2.
Pointers
5.3.
References 
6.
User Defined Data types
6.1.
Structure
6.2.
Union
6.3.
Enumeration 
7.
Frequently Asked Questions
8.
Key Takeaways: