×

C DATA TYPES


In this chapter, we will be learning about different data types in c language.


Why C ?

What is data type?

Each Variable in C is of some data type, which means we have to tell the compiler what kind of data we are going to store in a particular variable.

For example, consider the below variable declarations -

int sum;
char letter;

From the above declaration, the compiler will understand that -

In variable sum, integer type of data will be stored.

Similarly, in the variable letter, a character type will be stored.

The int and char types are known as Data Types in C. Now let's look at what are different types of data type present in C

Data type classifications

Classification

Description

Primary or Built-in data types

These are the data types which are pre-defined in c language such as int, char, float, double, void and so forth.

Derived data types

These are the data types which are inherited by the primary data types, for example, Pointers and arrays.

User-defined data types

These are the data types which are defined by the user(programmer). They are Structures, Union, Enumerations, and so forth.

Each data types holds some space in the memory. The table, displaying which Data Types acquire how much space. These values are for 16-bit Computer.

Data type

Size

Value range

int

2 or 4 bytes

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

unsigned int

2 or 4 bytes

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

short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

long int

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long int

4 bytes

0 to 4,294,967,295

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

float

4 bytes

3.4E-38 to 3.4E+38

double

8 bytes

1.7E-308 to 1.7E+308

long double

10 bytes

3.4E-4932 to 1.1E+4932


We use sizeof() operator to get the size of a data type

This chapter mainly focuses on Built-in Data Types. Derived Data Types will be explained in the upcoming tutorials.

Now let's write a simple c program to illustrate the use data type with sizeof() operator.


File - find_data_type_sizes.c
#include<stdio.h>
int main()
{

    printf("Storage size for unsigned int : %ld \n", sizeof(unsigned int));

    printf("Storage size for int : %ld \n", sizeof(int));

    printf("Storage size for short : %ld \n", sizeof(short));

    printf("Storage size for unsigned short : %ld \n", sizeof(unsigned short));

    printf("Storage size for long int : %ld \n", sizeof(long int));

    printf("Storage size for unsigned long int : %ld \n", sizeof(unsigned long int));

    printf("Storage size for char : %ld \n", sizeof(char));

    printf("Storage size for unsigned char : %ld \n", sizeof(unsigned char));

    printf("Storage size for signed char : %ld \n", sizeof(signed char));

    printf("Storage for float :%ld \n",sizeof(float));

    printf("Storage for double :%ld \n",sizeof(double));

    printf("Storage for long double :%ld \n",sizeof(long double));

    return 0;

}

Note: Below output is executed on a 64-bit Ubuntu, So values differ from the above chart.

Output
Storage size for unsigned int : 4
Storage size for int : 4
Storage size for short : 2
Storage size for unsigned short : 2
Storage size for long int : 8
Storage size for unsigned long int : 8
Storage size for char : 1
Storage size for unsigned char : 1
Storage size for signed char : 1
Storage for float :4
Storage for double :8
Storage for long double :16  

In the next Tutorial, We will learn about variables.

Sharing is caring: