Data Types in C Language

There is an extensive system that is used for declaring variables and functions in the C language. The variable types define how much space does it acquire in storage in bits. The types in C are as follows.

 

Types

Description

  • Basic Types

Basic types are also known as arithmetic types. They are further classified into (a) Integer types and (b) Floating-point types.

  • Enumerated types

It is also an arithmetic type that is used to define variables that can assign certain discrete integer values only.

  • Type void

The type specifier void indicates that no value is available.

  • Derived types

Derived type contains (a)Array types, (b)Pointer types, (c) Union types , (d)Structure types and (e) Function types.

 

Both the array and structure types are defined as aggregate types. Integer types are also widely used in the C language. Let's know in brief about Integer types.

 

Integer Types

Here we have specified the brief table of integer types along with their storage size and value ranges.

 

Storage size

Type

Range of Value

1 byte

char

-128 to 127 or 0 to 255

1 byte

unsigned char

0 to 255

1 byte

signed char

-128 to 127

2 or 4 bytes

int

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

2 or 4 bytes

 unsigned int

0 to 65,535 or 0 to 4,294,967,295

2 bytes

short

-32,768 to 32,767

2 bytes

unsigned short

0 to 65,535

8 bytes or (4bytes for 32 bit OS)

long

-9223372036854775808 to 9223372036854775807

8 bytes

unsigned long

0 to 18446744073709551615

 

To calculate the original size of a variable or type you can use the operator "sizeof" which helps you in finding the storage size of an object or type in bytes. Here is a program to find the size of a variable using C language.

 

Program-

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#include <float.h>

 

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

 

    printf("CHAR_BIT    -   %d\n", CHAR_BIT);

    printf("SHRT_MAX    -   %d\n", SHRT_MAX);

    printf("USHRT_MAX   -   %d\n", (unsigned short) USHRT_MAX);

    printf("CHAR_MIN    -   %d\n", CHAR_MIN);

    printf("LONG_MAX    -   %ld\n", (long) LONG_MAX);

    printf("INT_MAX     -   %d\n", INT_MAX);

    printf("INT_MIN     -   %d\n", INT_MIN);

    printf("LONG_MIN    -   %ld\n", (long) LONG_MIN);

    printf("SCHAR_MIN   -   %d\n", SCHAR_MIN);

    printf("SCHAR_MAX   -   %d\n", SCHAR_MAX);

    printf("SHRT_MIN    -   %d\n", SHRT_MIN);

    printf("CHAR_MAX    -   %d\n", CHAR_MAX);

    printf("UINT_MAX    -   %u\n", (unsigned int) UINT_MAX);

    printf("UCHAR_MAX   -   %d\n", UCHAR_MAX);

    printf("ULONG_MAX   -   %lu\n", (unsigned long) ULONG_MAX);

 

    return 0;

}

 

Output-

CHAR_BIT    -   8                                                                                

SHRT_MAX    -   32767                                                                            

USHRT_MAX   -   65535                                                                            

CHAR_MIN    -   -128                                                                             

LONG_MAX    -   9223372036854775807                                                              

INT_MAX     -   2147483647                                                                       

INT_MIN     -   -2147483648                                                                      

LONG_MIN    -   -9223372036854775808                                                             

SCHAR_MIN   -   -128                                                                             

SCHAR_MAX   -   127                                                                              

SHRT_MIN    -   -32768                                                                           

CHAR_MAX    -   127                                                                              

UINT_MAX    -   4294967295                                                                       

UCHAR_MAX   -   255                                                                              

ULONG_MAX   -   18446744073709551615 

 

 

Floating Types

There are floating types in C languages. Here we have mentioned all the floating types along with their sizes and values.

 

 

Type

Storage size

 Range of Value

Precision

float

4 byte

1.2E-38 to 3.4E+38

6 decimal places

double

8 byte

2.3E-308 to 1.7E+308

15 decimal places

long double

10 byte

3.4E-4932 to 1.1E+4932

19 decimal places

 

 

The Void Types

 

Types

Description

Function returns as void

There are several functions in the C language which do not return any value. A function with no return value has the return type as void. For example, void exit (int status);

Function arguments as void

There are several functions in the C language which do not accept any type of parameter. A function that has no parameter can accept the void. For example, int rand(void);

Pointers to void

A pointer of type void * displays the common address of an object, but not its type. For example, a memory allocation function void returns a pointer to void which can be set to any data type.