C – Data Types3 min read

In C programming, data types specify the varying sizes and values in the variables that can be stored. It allocates the memory to store on OS depending on its type.

A data type or simply type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support common data types of real, integer, and boolean.

There are two types of data type in C. They are:

  • Primary data-type (Primitive data-type)
  • Derived data-type (Non-primitive data-type)

1. Primitive data type also known as Primary data types or pre-defined data types.

These are int, char, float, void as shown in the table below.

Data typeSizeRangeDescription
char1 byte-128 to +127A character
int2 or 4 byte-32,768 to 32,767 or
-2,147,483,648 to +2,147,483,647
An integer
float4 byte1.2E-38 to 3.4E+38Single precision floating point number
void1 byte-128 to +127void type stores nothing

Character type (char):

It stores a single character and requires a single byte. It can store 128 characters. char keyword used to define the character type.

Such as: char ch = 'corn';

Integer data type (int): 

It is a data type that can only store integers value.int keyword is used to define integer type in C Language. Integer can have zero, positive or negative value but it cannot be float or decimal value.

The size of the integer type is 2 or 4 bytes and it can store value upto 32,768 to 32,767 or -2,147,483,648 to +2,147,483,647.

Such as int value_name;

Float type(float & double): 

It is used to store decimal numbers (numbers with floating-point value) with single precision. float or double the keyword is used to define real numbers type.

The size of a float is 4 bytes and the size of double is 8 bytes. a float can store value up to 1.2E-38 to 3.4E+38 whereas double store value up to 2.3E-308 to 1.7E+308.

Such as: float salary; double price;

Note that:C considers floating-point literal as double type. Add suffix f or F after floating-point literal to specify a type as a float.

void type(void): 

As the name suggests, void internally does not store anything. They are used to define a function return type or a generic pointer void * ptr;

It is used in three kinds of situations:

  1. Function returns as void
  2. Function arguments as void
  3. Pointers to void

2. Non Primitive data type also known as Derived data types:

These are : arrays, pointers, union, structures, etc.

Array: 

An array is a collection of data or finite ordered collection of homogeneous data, stored in contiguous memory locations. Arrays are also referred to as structured data types.

Pointers: 

The pointer is a variable the stores/points the address of another variable. The pointer is used to allocate memory dynamically. The other variable can be of type int, char, array function, or any other pointer.

Union: 

Union in C is also like structure, i.e. collection of different data types that are grouped together and the element is called a member.

Structure: 

The structure is a variable that gives the facility to store data of different data types in one variable which is not possible in an array. Structures are variables that have several parts. Each of those parts is called a member of the structure.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …