Data Types in C Language

The data type in c is nothing but type of a data. Each data type has a predefined memory size. The data type values are limited by memory size. This predefined memory is created for each variables when the program is running and destroyed at the end of the program.



Data Types in C

The data type in c language is broadly classified into four categories.

TypesDescriptionData types
BasicBasic data types are primary data types that stores values like number, character.int, float, char
DerivedDerived data types are created from the basic data types such as arrays, structures, union etc.array, Struct, Union, Pointers
EnumeratedThe enumerated data types are customized data types that contains discrete values. enum
voidvoid data types indicates such as empty, no value, not available.void


Basic data types in C

Basic data type sizes are measured using C-language bytes. Memory size depends on the 32-bit or 64-bit operating system.

Below are the basic data types used in the C-language.

Sr.NoData typeMemory SizeValue Range
1char
char1 byte −128 to 127
signed char1 byte−128 to 127
unsigned char1 byte0 to 255
2short
short2 byte −32,768 to 32,767
signed short2 byte−32,768 to 32,767
unsigned short2 byte0 to 65,535
3int
int4 bytes -2,147,483,648 to 2,147,483,647
signed int4 bytes -2,147,483,648 to 2,147,483,647
unsigned int4 bytes 0 to 4,294,967,295
4short int
short int 2 bytes −32,768 to 32,767
signed short int 2 bytes −32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
5long int
long int4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295
6decimal
float 4 bytes 1.17549e-038 to 3.40282e+038
double 8 bytes 2.22507e-308 to 1.79769e+308
long double 10 bytes 1.13305e-317 to 1.13304e+317


Program to find size, maximum value, minimum value of datatypes

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

int main(int argc, char** argv) {

    printf("char\n");
    printf("----\n");
    printf("char size         :   %d \n", sizeof(char));
    printf("char              :   %d to %d\n", CHAR_MIN, CHAR_MAX);
    printf("signed char       :   %d to %d\n", SCHAR_MIN, SCHAR_MAX);
    printf("unsigned char     :   %d to %d\n", 0, UCHAR_MAX);
    printf("\n");
    
    printf("short\n");
    printf("----\n");
    printf("short size        :   %d \n", sizeof(short));
    printf("short             :   %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("signed short      :   %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("unsigned short    :   %d to %d\n", 0, USHRT_MAX);
    printf("\n");

    printf("int\n");
    printf("----\n");
    printf("int size          :   %d \n", sizeof(int));
    printf("int               :   %d to %d\n", INT_MIN, INT_MAX);
    printf("signed int        :   %d to %d\n", INT_MIN, INT_MAX);
    printf("unsigned int      :   %d to %u\n", 0, (unsigned int) UINT_MAX);
    printf("\n");

    printf("long\n");
    printf("----\n");
    printf("long size         :   %d \n", sizeof(long int));
    printf("long              :   %ld to %ld\n", (long int) LONG_MIN, (long int) LONG_MAX);
    printf("signed long       :   %ld to %ld\n", (long) LONG_MIN, (long) LONG_MAX);
    printf("unsigned long     :   %ld to %lu\n", 0, (unsigned long) ULONG_MAX);
    printf("\n");

    return 0;
}

Output

char
----
char size         :   1
char              :   -128 to 127
signed char       :   -128 to 127
unsigned char     :   0 to 255

short
----
short size        :   2
short             :   -32768 to 32767
signed short      :   -32768 to 32767
unsigned short    :   0 to 65535

int
----
int size          :   4
int               :   -2147483648 to 2147483647
signed int        :   -2147483648 to 2147483647
unsigned int      :   0 to 4294967295

long
----
long size         :   4
long              :   -2147483648 to 2147483647
signed long       :   -2147483648 to 2147483647
unsigned long     :   0 to 4294967295


Float Datatype

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

int main(int argc, char** argv) {

    printf("float\n");
    printf("----\n");
    printf("float size        :   %d \n", sizeof(float));
    printf("float             :   %g to %g\n", FLT_MIN, FLT_MAX);
    printf("float Precision   : %d\n", FLT_DIG );
    printf("\n");

    printf("double\n");
    printf("----\n");
    printf("double size       :   %d \n", sizeof(double));
    printf("double            :   %g to %g\n", (double)DBL_MIN, (double)DBL_MAX);
    printf("\n");

    printf("long double\n");
    printf("----\n");
    printf("long double size  :   %d \n", sizeof(long double));
    printf("long double       :   %g to %g\n", (long double)LDBL_MIN, (long double)LDBL_MAX);
    printf("\n");

    return 0;
}

Output

float
----
float size        :   4
float             :   1.17549e-038 to 3.40282e+038
float Precision   : 6

double
----
double size       :   8
double            :   2.22507e-308 to 1.79769e+308

long double
----
long double size  :   16
long double       :   1.13305e-317 to 1.13304e-317



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *