Data Types in C

Data types in C: It is the collection of data with values having a fixed meaning as well as characteristics.  There are various data types in C language.

C Data Types Uses

  • Identify the type of a variable when it is declared.
  • Gives return value of a function
  • Describes the type of parameter expected by a function.

Data Types in C Language

C provides 3 types of data types. They are as follows:

  • Primary (Built-in) Data types: void, int, char, float and double.
  • Derived data types:  Arrays, References, and Pointers.
  • User Defined Data types: Structure, Union and Enumeration.

1) Primary Data types

Integer Data Type in C: Integers are used to store a whole number.

Size and range of Integer type on a 16-bit machine

Type Size (bytes) Range
int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed short int 1 -128 to 127
unsigned short int  1 0 to 255
long int or signed long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295

Floating Point Data Type in C

Floating Point data types are used to store real numbers.



Size and range of Floating type on a 16-bit machine

Type Size(bytes) Range
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932

And the header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs.

Example

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
    printf("Storage size for float : %d \n", sizeof(float));
    printf("FLT_MAX: %g\n",(float) FLT_MAX);
    printf("FLT_MIN: %g\n",(float) FLT_MIN);
    printf("-FLT_MAX: %g\n",(float) -FLT_MAX);
    printf("-FLT_MIN: %g\n",(float) -FLT_MIN);
    printf("DBL_MAX: %g\n",(double) DBL_MAX);
    printf("DBL_MIN: %g\n",(double) DBL_MIN);
    printf("-DBL_MAX: %g\n",(double) -DBL_MAX);
    printf("Precision value: %d\n", FLT_DIG );
    return 0;
}

Output
Storage size for float : 4
FLT_MAX : 3.40282e+38
FLT_MIN : 1.17549e-38
-FLT_MAX : -3.40282e+38
-FLT_MIN : -1.17549e-38
DBL_MAX : 1.79769e+308
DBL_MIN : 2.22507e-308
-DBL_MAX : -1.79769e+308
Precision value: 6

Character Data Type in C

Character data types are used to store the value of the character.

Size and range of Floating type on a 16-bit machine

Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255

Double Data Type in C

Double data types are the same as the float data type, which allows up to 10-digits after the decimal. While the range of double is 1E-37 to 1E+37.

Example

#include <stdio.h>
#include <limits.h>
int main()
{
   int a;
   char b;
   float c;
   double d;
   printf("Storage size for int data type:%d \n",sizeof(a));
   printf("Storage size for char data type:%d \n",sizeof(b));
   printf("Storage size for float data type:%d \n",sizeof(c));
   printf("Storage size for double data type:%d\n",sizeof(d));
   return 0;
}

Output
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

Void Data Type

Void data type means no value. Because it is usually used to specify the type of functions which returns nothing. We will learn more about this datatype as we deep learning in C language, like functions, pointers, etc.