C - Data Types


Advertisements

Previous Page Next Page

One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. Based on the data type of a variable, the operating system allocates memory to the variable and decides what can be stored in the reserved memory.

There are three types of data types in C which is categorized as below:

CategoryData Types
Basic Data Typeinteger, character, floating point, double floating point, etc.
Derived Data Typearray, pointer, function, reference, etc.
User Defined Data Typestructure, union, enum, typedef, etc.

The below table describes basic data types of C in detail:

CategoryData Types
characterchar
Integerint
Floating Pointfloat
Double Floating Pointdouble

There are several types of modifier which can be used to modify the data types in C++.

  • signed
  • unsigned
  • short
  • long

The below table describes the variable type, memory size, format specifier, and maximum and minimum value which can be stored in the variable.

CategoryMemory SizeFormat SpecifierRange
char1 byte%c-128 to 127
unsigned char1 byte%c0 to 255
signed char1 byte%c-128 to 127
short int 2 byte %hd-32,768 to 32,767
unsigned short int 2 byte %hu0 to 65,535
signed short int 2 byte %hd-32,768 to 32,767
int4 byte%d-2,147,483,648 to 2,147,483,647
unsigned int4 byte%u0 to 4,294,967,295
signed int4 byte%d-2,147,483,648 to 2,147,483,647
long int 8 byte %ld-2,147,483,648 to 2,147,483,647
unsigned long int 8 byte %lu0 to 4,294,967,295
signed long int 8 byte %ld-2,147,483,648 to 2,147,483,647
long long int 8 byte %lld-(2^63) to (2^63)-1
unsigned long long int 8 byte %llu0 to 18,446,744,073,709,551,615
float 4 byte %f
double 8 byte %lf
long double 12 byte %Lf

sizeof() function

The memory size of a data type might be different as shown in the above table, depending upon the particular platform. To find out the size of a data type, sizeof() function can be used.

#include <stdio.h>
 
int main (){
   printf("Size of char: %d\n", sizeof(char));
   printf("Size of short int: %d\n", sizeof(short int));
   printf("Size of int: %d\n", sizeof(int));
   printf("Size of long int: %d\n", sizeof(long int));
   printf("Size of float: %d\n", sizeof(float));
   printf("Size of double: %d\n", sizeof(double));
   return 0;
}

Output

Size of char: 1
Size of short int: 2
Size of int: 4
Size of long int: 8
Size of float: 4
Size of double: 8

Previous Page Next Page
Advertisements