Data Types in C

A data type in C refers to the type of data used to store the information. For example, the name of a person would be an array of characters, while the age will be in integers. Whereas, the marks of a student would require a data type that can store decimal values.In C language, four different data types can be used to differentiate and store various types of data. They are given in the table below:

Basic Data Types:-

In C language, basic data types are used to store values in integer and decimal forms. It supports both signed and unsigned literals. There are four basic data types, in both signed and unsigned forms:

  • Int
  • Float
  • Double
  • Char

1.Integer Data Type:-

An integer type variable can store zero, positive, and negative values without any decimal. In C language, the integer data type is represented by the ‘int’ keyword, and it can be both signed or unsigned. By default, the value assigned to an integer variable is considered positive if it is unsigned.

The integer data type is further divided into short, int, and long data types. The short data type takes 2 bytes of storage space; int takes 2 or 4 bytes, and long takes 8 bytes in 64-bit and 4 bytes in the 32-bit operating system.

If you try to assign a decimal value to the integer variable, the value after the decimal will be truncated, and only the whole number gets assigned to the variable. Here is an example that will help you understand more about the concept.

#include <stdio.h>   
void main() 
{ 
 int i = 5; 
 printf("The integer value is: %d \n", i); 
} 

2. Float-Pointing Data Types:-

The floating-point data type allows a user to store decimal values in a variable. It is of two types:

  • Float
  • Double

Float variables store decimal values with up to 6 digits after the decimal place. The storage size of the float variable is 4 bytes, but the size may vary for different processors, the same as the ‘int’ data type.

In C language, the float values are represented by the ‘%f’ format specifier. A variable containing integer value will also be printed in the floating type with redundant zeroes. It will be clear for you from the example given below:

#include <stdio.h>
int main() {
	float sum=9664.35;
	float num=67;
	float average=(sum/num);
	printf("Average is %f \n", average);
	printf("Value of num is %f \n",num);
	printf("Value of num presented as an integer %d \n",num);
}

Double
The double data type is similar to float, but here, you can have up to 10 digits after the decimal place. It is represented by the ‘double’ keyword and is mainly used in scientific programs where extreme precision is required.

In the example below, the double variable prints the exact value ‘349999999.454’, while the float variable messes up with the number and prints a round-off value.

#include <stdio.h>
int main()
{
	float score=349999999.454;
	double average=349999999.454;
	printf("Score in %f \n",score);
	printf("Average is %lf",average);
	return 0;
}

3.Char:-

Char is used to storing single character values, including numerical values. If you create an array of the character data type, it becomes a string that can store values such as name, subject, and more.

Here is an example of creating an array of characters in C programming.

#include
int main()
{
	char group='A';
	// to store a string of characters in C programming
	char name[30]="student";
	printf("The group is %c \n",group);
	printf("The name is %s",name);
	return 0;
}

Derived Data Types:-

Derived data types are primary data types that are grouped together. You can group many elements of similar data types. These data types are defined by the user. The following are the derived data types in C:

  • Array
  • Pointers
  • Structure
  • Union

1. Array:-

An array in C is a collection of multiple values of a similar data type and is stored in a contiguous memory location. An array can consist of chars, integers, doubles, etc.

Declaration of Array in C:

data_type array_name[array_size];

Array Example:-

#include<stdio.h>   
int main(){ 
int i=0; 
int marks[5];//declaration of array 
clrscr(); 
marks[0]=50;//initialization of array 
marks[1]=60; 
marks[2]=75; 
marks[3]=40; 
marks[4]=85; //traversal of array 
for(i=0;i<5;i++){ 
printf("%d",marks[i]); } 
return 0; 
}

Output:

50 60 75 40 85

2. Pointers:-

The pointer data type is used to store the address of another variable. A pointer can store the address of variables of any data type. Pointers allow users to perform dynamic memory allocation. They also help to pass variables by reference.

A pointer with no address is called a null pointer. A pointer with no data type is a void Pointer. It is defined by using a ‘*’ operator.

Example – Program to illustrate Pointer

int main(void) { 
int *ptr1; 
int *ptr2; 
int a = 5; 
int b = 10; 
//address of a is assigned to ptr1 
ptr1 = &a; 
//address of b is assigned to ptr2 
ptr2 = &b; 
//display value of a and b 
printf("%d", *ptr1); //prints 5 
printf("\n%d", *ptr2); //prints 10 
//print address of a and b 
printf("\n%d", ptr1); // prints address 
printf("\n%d", ptr2); // prints address 
//pointer subtraction 
int minus = ptr2 - ptr1; 
printf("\n%d", minus); //prints the difference 
return 0; } 

3. Structure:-

It is a data type that can store variables of similar or different data types. For example, we can use structures to store information about an employee, such as the employee name, employee ID, salary, and more. Each employee’s record will be represented by an object of the structure. The size of the structure is the sum of the storage size required by each variable. The ‘struct’ keyword defines a structure.

Example – Program to illustrate Structure

#include <stdio.h>   
#include <string.h> 
struct Employee { char name[50]; 
int emp_id; 
float salary; } employee1; 
int main() { 
strcpy(employee1.name, "John"); 
employee1.emp_id = 1779; 
employee1. salary = 3900; 
printf("Name: %s\n", employee1.name); 
printf("Employee ID: %d\n", employee1.emp_id); 
printf("Salary: %.2f", employee1.salary); 
return 0; }

4. Union:-

A union is a group of elements with similar or different data types. In a union, the memory location is the same for all the elements. Its size will be equal to the memory required for the largest data type defined. We use the keyword ‘union’ to define a union. You can declare many variables. However, just one variable can store the value at a time.

Example – Defining a Union

union Student{
int id;
char name[20];
float marks[5];
}
st1, st2;

Enumerated Data Types:

Enumerated data types are user-defined data types that consist of integer values. They are used to define variables that can only assign certain discrete integer values in the program. They are used to make a program more readable, flexible, and maintainable. We use the keyword ‘enum’ to declare new enumeration types in the C programming language.

Enum syntax:

enum flag {const1, const2, const3………};


A popular example of enumerated data types is the days of the week.

Example – Program to illustrate Enumeration

#include<stdio.h>   
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; 
int main() 
{ 
enum week day; 
day = Fri; 
printf("%d",day); 
return 0; 
} 

Output:-

4

Void Data Types :

The void is just an empty data type that depicts that no value is available. Typically, the void is used for functions. When we declare a function as void, it doesn’t have to return anything.

Void is used in three situations:

  • Function returns as void – A function with no return value will have return type as void.
  • Function arguments as void – A function with no parameter can accept the void.
  • Pointers to void – It represents the address of an object, but not its type.
  • Example – The below function will not return any value to the calling function.

void sum (int a, int b);

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads