Data Types in C

C programming language is full of different data types. Data types determine the type of data we want to store in a variable to perform a specific operation in a program.

  • Different data types have different storage representations and machine instruction to handle data in a variable.
  • Data types allow the user to select the type to perform a specific operation needed in the program or application.

There are three classes of data types in C programming language:

  1. Primary data types : It includes the fundamental data types used in a program such as integer (int), character (char), floating point (float), double floating point (double) and void. Many of these can be extended further as per requirement into long data types like long int and long double.
  2. Derived data types : It is a derived version of fundamental data types that make it easy for a user to perform complex operations quickly. E.g. – Arrays, Functions, Structures and Pointers.
  3. User-defined data types : This type of data type is defined by the user as per the requirement that arises from time to time during the execution of the program. E.g. – Structures and Unions.

Different data types in C language

Integer type – A variable of integer data type stores an integer.

Character type – A variable of character data type stores a single character.

Float type – A variable of float data type stores a decimal number with single precision.

Double type – A variable of double data type stores decimal number with double precision.

In order to perform control over the range of numbers, C programming language has three classes of integer values in integer types with both signed and unsigned forms:

  • short int
  • int
  • long int

Similarly, Character type is further divided into two classes on the basis of range:

  • unsigned char
  • signed char

Also, Float type is extended further into two classes for providing more precision:

  • double
  • long double

The list of size and range of Data Types in C in a 32-bit machine is listed in the table below:

Example:

char Name;
int RollNum;
int Age;
float Marks;
double pi_d = 3.14159265358979323846264L;
long double pi_ld = 3.14159265358979323846264L;
unsigned short int usit = 65535;

Explanation:

The data type char (character) signifies that Name is a character type variable.

The data type int (integer) signifies that RollNum is an integer type variable.

The data type int (integer) signifies that Age is an integer type variable.

The data type float signifies that Marks is a float type variable.

The data type double signifies that pi_d is a double type variable.

The data type long double signifies that pi_ld is a double type variable with more precision.

The data type unsigned short int signifies that usit is aninteger type variable with more accuracy.

Program to demonstrate the use of data types in C

Example:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
int main(int argc, char **argv)
{
      freopen("output.txt","w",stdout);

  float pi_f = 3.14159265358979323846264L;
  double pi_d = 3.14159265358979323846264L;
  long double pi_ld = 3.14159265358979323846264L;
  printf("PI              %1.23f\n", pi_f);
  printf("PI              %1.23f\n", pi_d);
  printf("PI              %1.23Lf\n", pi_ld);
  printf("PI Original     3.14159265358979323846264\n");
  printf("Size of float data type : %lu\n", sizeof(float));
  printf("Size of double data type : %lu\n", sizeof(double));
  printf("Size of long double data type : %lu\n", sizeof(long double));

  unsigned short int usit = 65535;
  unsigned int uit = 4294967295;
  unsigned long int ulit = 18446744073709551615U;
  /*Usigned min value is 0 */
  printf("Max value of unsigned short int data type : %hu\n", usit);
  printf("Max value of unsigned int data type : %u\n", uit);
  printf("Max value of unsigned long int data type : %lu\n", ulit);
  printf("Size of unsigned short int data type : %lu\n", sizeof(unsigned short int));
  printf("Size of unsigned int data type : %lu\n", sizeof(unsigned int));
  printf("Size of unsigned long int data type : %lu\n", sizeof(unsigned long int));


   short int sit = 32767;
   int it = 2147483647;
   long int lit = 9223372036854775808U;
  printf("Max value of unsigned short int data type : %hd\n", sit);//-(2^15) to (2^15)-1
  printf("Max value of unsigned int data type : %d\n", it); //-(2^31) to (2^31)-1
  printf("Max value of unsigned long int data type : %ld\n", lit); //-(2^63) to (2^63)-1
  printf("Size of short int data type : %lu\n", sizeof(short int));
  printf("Size of int data type : %lu\n", sizeof(int));
  printf("Size of long int data type : %lu\n", sizeof(long int));
  


  signed char cccc= 70;
  unsigned char cccc1= 70;
    printf("char signed : %c\n",cccc);
    printf("char unsigned signed : %c\n",cccc1);

  printf("Size of  unsigned char data type : %lu\n",sizeof( unsigned char));
  printf("Size of  signed char type : %lu\n",sizeof(  signed char));


  return 0;
}

Output:

PI              3.14159274101257324218750
PI              3.14159265358979311599796
PI              3.14159265358979323851281
PI Original     3.14159265358979323846264
Size of float data type : 4
Size of double data type : 8
Size of long double data type : 16
Max value of unsigned short int data type : 65535
Max value of unsigned int data type : 4294967295
Max value of unsigned long int data type : 18446744073709551615
Size of unsigned short int data type : 2
Size of unsigned int data type : 4
Size of unsigned long int data type : 8
Max value of unsigned short int data type : 32767
Max value of unsigned int data type : 2147483647
Max value of unsigned long int data type : -9223372036854775808
Size of short int data type : 2
Size of int data type : 4
Size of long int data type : 8
char signed : F
char unsigned signed : F
Size of  unsigned char data type : 1
Size of  signed char type : 1