A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

There are the following data types in C language.

TypesData Types
Basic Data Typeint, char, float, double
Derived Data Typearray, pointer, structure, union
Enumeration Data Typeenum
Void Data Typevoid

 

Example Of Data Types In C

Let’s consider a scenario of a company. A company stores various data of their employee such as Name, Employee ID, Age, Salary, Address, Phone No, etc.

Now, these data are values containing alphabets, numbers, etc, so to make the processing of these huge data for programs easy, the information was categorized into different types:

  1. Name: String
  2. ID: Integer
  3. Salary: Float or Double
  4. Phone No: String 

Write a C program to perform input/output of all basic data types

				
					#include<stdio.h>

int main()
{
    /* declare all data types */
    
    char charVariable;
    unsigned char uCharVariable;
    
    short shortVariable;
    unsigned short uShortVariable;
    
    int intVariable;
    unsigned int uIntVariable;
    
    long longVariable;
    unsigned long uLongVariable;
    
    long long longLongVariable;
    unsigned long long uLongLongVariable;
    
    float floatVariable;
    double doubleVariable;
    long double longDoubleVariable;
    
    /*read input*/
    
    printf("Enter a character: ");
    charVariable = getchar();
    getchar(); 
    
    printf("Enter another character: ");
    uCharVariable = getchar();
    getchar(); 
    
    printf("Enter a signed short value: ");
    scanf("%hi", &shortVariable);
    
    printf("Enter an unsigned short value: ");
    scanf("%hu", &uShortVariable);
    
    printf("Enter an signed integer value: ");
    scanf("%d", &intVariable);
    
    printf("Enter an unsigned integer value: ");
    scanf("%d", &uIntVariable);
    
    printf("Enter a signed long value: ");
    scanf("%ld", &longVariable);
    
    printf("Enter an unsigned long value: ");
    scanf("%lu", &uLongVariable);
    
    printf("Enter a signed long long value: ");
    scanf("%lld", &longLongVariable);
    
    printf("Enter an unsigned long long value: ");
    scanf("%llu", &uLongLongVariable);
    
    printf("Enter a float value: ");
    scanf("%f", &floatVariable);
    
    printf("Enter a double value: ");
    scanf("%lf", &doubleVariable);
    
    printf("Enter a long double value: ");
    scanf("%Lf", &longDoubleVariable);
    
    
    /* print the value of all variable */
    
    printf("\nYou entered character: '%c' \n", charVariable);
    printf("You entered unsigned character: '%c' \n\n", uCharVariable);
    
    printf("You entered signed short: %hi \n", shortVariable);
    printf("You entered unsigned short: %hu \n\n", uShortVariable);
    
    printf("You entered signed int: %d \n", intVariable);
    printf("You entered unsigned int: %d \n\n", uIntVariable);
    
    printf("You entered signed long: %ld \n", longVariable);
    printf("You entered unsigned long: %lu \n\n", uLongVariable);
    
    printf("You entered signed long long: %lld \n", longLongVariable);
    printf("You entered unsigned long long: %llu \n\n", uLongLongVariable);
    
    printf("You entered float: %f \n", floatVariable);
    printf("You entered double: %lf \n", doubleVariable);
    printf("You entered long double: %Lf \n", longDoubleVariable);

    return 0;
}

				
			

Size Of Data Types In C

The size of each data type is defined in bits or bytes (8 bits). Each data type in C is associated with a specific range of values defined as below: 

Data TypeFormat SpecifierMinimal RangeSize in bit
unsigned char%c0 to 2558
char%c-127 to 1278
signed char%c-127 to 1278
int%d, %i-32,767 to 32,76716 or 32
unsigned int%u0 to 65,53516 or 32
signed int%d, %i-32,767 to 32,767 (same as int)16 or 32
short int%hd-32,767 to 32,76716
unsigned short int%hu0 to 65,53516
signed short int%hdSame as short int16
long int%ld, %li-2,147,483,647 to 2,147,483,64732
long long int%lld, %lli-(2^63) to (2^63)-164
signed long int%ld, %liSame as long int32
unsigned long int%lu0 to 4,294,967,29532
unsigned longlong int%llu(2^63)-164
float%f1E-37 to 1E+37 along with six digits of the precisions 32
double%lf1E-37 to 1E+37 along with six digits of the precisions 64
long double%Lf1E-37 to 1E+37 along with six digits of the precisions 80

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

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

				
			
				
					#include<stdio.h>   
int main()
{ 
int i=0; 
int marks[5];//declaration of array 

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 \n",marks[i]);
} 
return 0; 
}
				
			

Pointer Data Type

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.

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

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’s 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.

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

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.

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

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

Void

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

				
			
Spread the love