Skip to main content

Data types in C Programming

What is a Data Type

A data type determines the type of value that can be stored in a variable and the operations that can be performed on that variable.

C has a variety of built-in data types, including integers, floating-point numbers, characters, and arrays. There are also derived data types, such as pointers and structures, which are formed by combining basic data types in different ways.

Basic data types

In the C programming, there are several built-in data types that you can use:

  • char: a character data type that can store a single character.

  • int: an integer data type that can store whole numbers.

  • float: a floating-point data type that can store decimal numbers.

  • double: a double-precision floating-point data type that can store very large or very small decimal numbers with greater precision than the float data type.

Derived Data types

In addition to these basic data types, C also has derived data types, which are formed by combining basic data types in different ways. Here are some examples of derived data types:

  • array: an array is a collection of variables of the same data type that are accessed using an index.

  • pointer: a pointer is a variable that stores the address of another variable.

  • structure: a structure is a data type that can store a group of variables of different data types under a single name.

  • union: a union is a data type that can store a group of variables, but only one of them can be used at a time.

  • enum: an enumeration is a data type that consists of a set of named constants.

typedef keyword

You can also define your own custom data types using the typedef keyword, which allows you to give a new name to an existing data type.

For example, you could use typedef to define a new data type called age as follows:

typedef int age;

With this typedef statement, you can then use the age data type to declare variables that can store integer values representing a person's age.

Datatypes, Size and Range

Here is a table of the basic data types in C along with their Size and Range:

Data TypeDescriptionSize (in bytes)Range
charcharacter1-128 to 127 or 0 to 255
intinteger4-2147483648 to 2147483647
floatfloating-point41.2E-38 to 3.4E+38
doubledouble-precision floating-point82.3E-308 to 1.7E+308
tip

The size and range of these data types can vary depending on the specific compiler and system that you are using

Example to demonstrates the size and range

Below is an example of a program in C that demonstrates the size and range of the basic data types:

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

int main()
{
printf("Size of char: %d bytes\n", sizeof(char));
printf("Size of int: %d bytes\n", sizeof(int));
printf("Size of float: %d bytes\n", sizeof(float));
printf("Size of double: %d bytes\n", sizeof(double));

printf("\n");

printf("Range of char: %d to %d\n", CHAR_MIN, CHAR_MAX);
printf("Range of int: %d to %d\n", INT_MIN, INT_MAX);

return 0;
}
Output:

Exmplanation:

  • This program uses the sizeof operator to determine the size of each data type in bytes.

  • CHAR_MIN and related macros are defined in limits.h header file, and determine the minimum and maximum values that can be stored in each data type.

When you run this program, it will print the size and range of the basic data types as follows:

Size of char: 1 bytes
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes

Range of char: -128 to 127
Range of int: -2147483648 to 2147483647
tip

The size and range of these data types can vary depending on the specific compiler and system that you are using.