Data Types in C

Data Types in C

What is Datatypes ?

Data types are a set of values and are determined to act on those values. The type of a variable determines how much space it occupies in storage. 
In short data types are used to specify the type of data. The value of a variable can be changed any time.

In this tutorial you will know about :

Why do we need Data Types ?

1. To identify the type of a variable when it is declared.
2. To Identify the type of a parameter expected by a function.
3. To Identify the type of the return value of a function.

How to declare Data Types ?

The taken variable names are needed to be assigned with a data type. This is how the data types are used along with variables :

Example :
int eruditors;
float eru;
char team, eruditors;

Divisions of Data Types :

C Programming Language has 4 types of data types.
1. Primary/Basic Data Types : int, float, char, double.
2. Derived Data Types : array, pointer, function.
3. Enumeration Data Types : enum.
4. Void Data Types : void.

1. Primary/Basic Data Types : 

This primitive data types are divided into 4 parts. They are :

A. Integer Data Types : 

Integer is defined as a number that can have both zero, positive and negative values but has no fractional component.

Example :
0, -47, 91

Key Points about Integer Data Types :
~ Integer data is represented by its own datatype known as int.
~ It requires 2 bytes of memory for 16bit compilers and 4 bytes of memory for both 32 & 64 bit compilers.
~ The Range of Integer Data Type : -32,768 to 32,767

B. Float and Double Data Types :

Numbers which have a fractional component is known floating point numbers or float. Both float & double are used to hold real numbers.

Example :
117.56, 101.211

Key Points about Float Data Types :
~ Float data is represented by its own data type known as float.
~ It requires 4 bytes of memory in almost all compilers.

Key Points about Double Data Types :
~ Double data is represented by its own data type known as double.
~ It requires 8 bytes of memory in almost all compilers.

Difference between Integer and Float Data Types :
1. Integers consumes significantly less space than Floating point numbers.
2. Floating point numbers can represent numbers accurately.

C. Character Data Types :

It is used to store characters and letters.

How to use character data types :
Syntax :
char team = 'j';

Key Points about Character Data Types :
~ Character data is represented by its own data type known as char.
~ It requires 1 byte of memory.
~ The character is also integer type because underneath C Programming Language stores integer numbers instead of characters. In order to represent characters, the computer has to map each integer with a corresponding character using a numerical code.
For example, the integer number 97 represents the 'a' character in lower case.

2. Derived Data Types :

Data types which are derived from fundamental data types are known as derived data types. This derived data types are divided into 5 parts. Those are :

A. Array :

An array is a collection of similar data items stored at contiguous memory locations and the array elements can be accessed randomly using index of that array. In simple word an array is a variable that can store multiple values of same data type.

B. Pointer :

A pointer is a special variable whose value is the address of another variable. In simple word, Pointer is also kind of a variable that is used to store address of variables or a memory location.

C. Function :

A function is a block of code that takes input, performs some specific tasks and produces output.

3.Enumeration Data Types :

Enumeration is a user defined data type which is used to assign names to integral constants. It makes a program more readable and maintained.

How to use enumerated data types :
Syntax :
enum tagname
   identifier1, identifier2,…….,identifier n

Example :
enum week{Sun, Mon, Tue, Wed, Thur, Fri, Sat}; 
int main()
    enum week day;
    day = Fri;
    return 0;

//here we declared "day" as the variable and "Fri" is allocated to "day".

Output :
//The value of "Fri" is allocated to "day" which is 5.
//So 5 is printed as output.

Key Points about Enumerated Data Types :
~ Enumerated data is represented by its own datatype known as enum.
~ By default, the compiler assigns values starting from 0. In the upper program "Mon" gets value 0, "Tue" 1 and so on. The default values of enum elements are changeable during declaration.
~ Values can be assigned to some name in any order. All unassigned names get value after the previous value's name +1

Example :
enum week{Sun=2, Mon, Tue, Wed=8, Thur, Fri=11, Sat};
int main()
    printf("%d %d %d %d %d %d %d",Sun, Mon, Tue, Wed, Thur, Fri, Sat);  
    return 0;

Output :
2 3 4 8 9 11 12
~ The assigned values to enum names must be some integral constant. The range must be from minimum possible integer value to maximum possible integer value.
~ All the enum constants must be unique in their scope.

4. Void Data Types :

 Datatype that is used to specify the type of functions which returns nothing is known as Void datatypes. This datatype is used in three kinds of situation :

A. Function returns as void :

A function with no return value has the return type as void. 

Example :
void exit ( int hello );

B. Function arguments as void :

Various types of function does not accept any parameter. The function with no parameter can accept a void.

Example :
>float aqua ( void );

C. Pointers to void :

A void type pointer (*) represents the address of an object, but not its type.

Example :
void *malloc ( position_t size );
//This memory allocation function returns a pointer to void which can be casted to any datatype.