C Data Types

A data type in C programming language declares the type of data that a variable can store and how much memory is required to store this data. The data type also defines the format in which a data of particular type should be stored.

For Example
  • character
  • integer etc.

C Data types can be classified into four categories
  • Basic Data types : There are four basic data types in C(int, char, float and double.)

  • Derived Data Type : Data types that are derived from fundamental data types are called derived data types(Array, Structures, Unions and Pointers).

  • Void Data Type : Void is an empty data type that has no associated value(void).

  • Enumerated Data Type : Enumeration data type consists of set of named constant values(enum).

C Basic Data Types

There are four basic data types in C programming language.

  • Character  (char)
  • Integer  (int)
  • Floating Point  (float)
  • Double Floating Point  (double)
All other data types are derived from these basic data types.

The size of basic data types are machine dependent, For example size of an integer data type in a 32 bit computer is 4 bytes whereas size of integer data type in 16 bit computer is 2 bytes. To know the exact size of any data type, we should use sizeof operator.


C Data Type Modifiers

Modifiers in C specifies the amount of memory space to be allocated for a variable. Modifiers are prefixed with basic data types to modify the amount of memory allocated for a variable.

For Example in a 16 bit system, the size of int data type is 2 bytes. If we add long prefix in integer variable declaration(long int), it's size becomes 8 bytes.

There are five data type modifiers in C Programming Language:
  • long
  • short
  • signed
  • unsigned
  • long long

Here, we will discuss about Basic Data types only. We will cover other data types in upcoming tutorials.


The following table, shows the different basic data types, their size and value range in a 16 bit operating system.
Data Types Memory Size in bytes Min-Max Range
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 32,767
signed int 2 bytes -32,768 to 32,767
short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 32,767
signed short int 2 bytes -32,768 to 32,767
long int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295
signed long int 4 bytes –2,147,483,647 to 2,147,483,647
char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
signed char 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
signed short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 32,767
float 4 bytes 1E–37 to 1E+37 with six digits of precision
double 8 bytes 1E–37 to 1E+37 with ten digits of precision
long double 10 bytes 1E–37 to 1E+37 with ten digits of precision

C Character Data Type

Character data type is used to store a character. A variable of character data type allocated only one byte of memory and can store only one character. Keyword char is used to declare variables of type character. Range of character(char) data type is -128 to 127.

For Example
    char c = 'A';
Above statement declares a variable 'c' of character data type and stores character 'A' in it's memory location.

Character Data Type Variations
  • char
  • unsigned char
  • signed char

C Integer Data Type

Integer data type is used to store a value of numeric type. Memory size of a variable of integer data type is dependent on Operating System, For example size of an integer data type in a 32 bit computer is 4 bytes whereas size of integer data type in 16 bit computer is 2 bytes.

Keyword int is used to declare variables of type integer. Range of integer(int) data type in 16 Bit system is -32,768 to 32,767.

For Example
    int i = 2015;
Above statement declares a variable 'i' of integer data type and stores 2015 in it's memory location.

Integer Data Type Variations
  • int
  • unsigned int
  • signed int
  • short int
  • unsigned short int
  • signed short int
  • long int
  • unsigned long int
  • signed long int

C Floating Point Data Type

Floating point data type can be sub-divided into two types on the basis of precision and size.

float 4 bytes with six digits of precision
double 8 bytes with ten digits of precision
float data type

Floating point data type in C is used to store a value of decimal values. Memory size of a variable of floating point data type is dependent on Operating System, For example size of an floating point data type in a 16 bit computer is 4 bytes. Keyword float is used to declare variables of floating point type. Floating point data type provides up-to 6 digits of precision.

For Example
    float f = 11.243567;
Above statement declares a variable 'f' of float data type and stores 11.243567 in it's memory location.


double data type

Floating point data type similar to float data type except it provides up-to ten digit of precision and occupies eight bytes of memory.

For Example
    double d = 11676.2435676542;
Above statement declares a variable 'd' of double data type and stores 11676.2435676542 in it's memory location.


C Program to Show Size of Basic Data Types

#include <stdio.h>
#include <limits.h>

int main(){
   /* 
    * Size of Data type is dependent on Operating System
    * I am running this program in a 32 bit OS 
    */
   printf("Size for char : %d \n", sizeof(char));
   printf("Size for int : %d \n", sizeof(int));
   printf("Size for float : %d \n", sizeof(float));
   printf("Size for double : %d \n", sizeof(double));
   
   return 0;
}

Output
Size for char : 1
Size for int : 4
Size for float : 4
Size for double : 8

Derived Data Types

Derived data types are created by combining basic data types or other derived types.
  • Array : An array is a collection of elements, all of the same data type. Elements are stored in contiguous memory locations, and each element is accessed by its index.
    int numbers[5] = {1, 2, 3, 4, 5};
    

  • Pointer : A pointer is a variable that stores the address of another variable. It allows dynamic memory allocation and manipulation.
    int x = 10;
    int *ptr = &x; // ptr now holds the address of x
    

  • Structure : A structure is a data type that is defined by the user and permits the aggregation of variables of various data types under a single name.
    struct Student {
        char name[50];
        int age;
        float gpa;
    };
    struct Student s1;
    

  • Union : Similar to a structure, a union stores its members' data in the same region in memory. Values are held by just one person at a time.
    union Data {
        int intValue;
        float floatValue;
        char stringValue[20];
    };
    union Data data1;
    

Type Casting

Type casting is the process of converting a variable from one data type to another. It can be implicit or explicit.
  • Implicit Type Casting : Also known as "coercion," implicit type casting is done automatically by the compiler.
    int intValue = 10;
    // Implicit casting from int to float
    float floatValue = intValue; 
    

  • Explicit Type Casting : Explicit type casting is done by the programmer using casting operators.
    float floatValue = 3.14;
    // Explicit casting from float to int
    int intValue = (int) floatValue;