Data Types and Their Ranges in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Data types in C (or in any programming language), are very important to learn and understand before you start to write a program.

They are needed to store different data types like integers, characters, decimals, strings or even user-defined.

Scope

  • In this article, we will look at why we need data types.
  • We will understand different categorizations of data types on different levels.
  • We will look at all the different types of primary data types and the range of data they can store.
  • We will also understand and learn how we can code our own secondary data types.

Why we need data types in C

We know that computers store all the data in the form of binary numbers, and it assigns memory to each of them. Now suppose you want to make a program to store your name, age and phone number. Without mentioning the data types, your computer would not be able to distinguish between your name, age and phone number and will treat them equally by assigning the same memory and keeping them in the same set of variables.

The age consists of the utmost 2 to 3 digits and the phone number consists of at least 10 digits, but computers will assign the same memory to both of them that will lead to a lot of memory wastage.

To deal with such scenarios, we assign data types to each variable to prevent any confusion or memory wastage.

What are data types in C

Data type is an attribute of data which tells the C compiler, which type of data a variable is holding.

It can be of type integer, float( decimal), character , boolean( true/false ) etc.

Formally we use data types to specify the type of data our variables are holding.

Broadly there are two types of data types in C:

a. Primary Data types or Basic Data Types b. Secondary Data Types c data types

A. Primary Data Types or Basic Data types

These are the most basic data types and all the other data typed are derived or made from them only. It contains integer, floating point and char.

Four main types of primary/basic data types are:

  1. Integer
  2. Float
  3. Char
  4. Void

Now, these are further classified as short, long,double, long double, signed and unsigned data types in C.

Before discussing this, let us first understand what short, long, signed and unsigned data types in C means.

SHORT AND LONG

These are used to define the amount of memory space that the compiler will allocate. In case of short int, it is typically 2 bytes and in long int, it is 4 bytes.

SIGNED AND UNSIGNED

This also deals with memory allocation only but in a different way. In case of signed int, it takes into account both negative and positive numbers. But in unsigned int, we can only represent positive numbers. Now since the range of values gets decreased in unsigned, thus it is able to represent higher values in the same memory prospect.

Let us understand it through an example, Integer data type consists of 2-byte memory.

1 Byte= 8 bits 2 Byte= 16 bits

The number of digits = 2^(2 Byte)= 2^(16 bits)= 65536

In the case of unsigned int data type, there are only positive numbers and since zero is also included in positive numbers therefore unsigned int can take values from 0 to 65535.

Now let us consider the case of signed data type, it has both positive and negative numbers. Thus the total present memory length gets divided into two equal halves (65536/2= 32768), one half to represent positive numbers ( 0 to 32767 )and other half to represent negative numbers (-1 to -32768).

This is applicable to all other types of data types like float char etc.

Now similarly, you can find the range of values of all data types like float, char, double etc.

Just pick up a pen and paper and find the range of signed char, unsigned char, signed float and unsigned float.

Hope you enjoyed the exercise!!!

LONG AND LONG DOUBLE

These are mostly used with decimal numbers. By using these prefixes, we can increase the range of values represented in float. Float is of 4 bytes, double is of 8 bytes and long double is of 10 bytes.

By using the relation mentioned above(int data type), we can calculate the length of the number in float and decimal.

For example, float takes 4 bytes, that is, 32 bits(4*8)

Therefore the length of number will be: 2^(32)= 4,29,49,67,296.

In case of double, it takes 8 bytes, 64 bits(8*8)

Therefore the length of number will be: 2^(64)= 1,84,46,74,40,73,70,95,51,616.

primary data type in c

1. CHAR DATA TYPE IN C

It is used to store a single character and requires 1 byte. A character could be any alphabet, number or special character written inside a pair of single inverted commas, eg ‘1’, ‘a’, ‘#’ etc.

Since it requires 1 Byte, which is 8 bits, the number of characters in C language is 256(2^8). Memory space differs with use of prefixes ( Hope you remember by the exercise we did above).

Output

2. INT DATA TYPE IN C

It is used to store integer values and requires memory according to the value of the integer we want to store.

The size of int is compiler dependent. For example, 32-bit compilers have int as 4 bytes but 64 bits compilers(which we are using now) have int as 8 bytes.

And since the size varies the range of values integers can store also varies from compiler to compiler.

For 2 bytes, it’s 0 to 65,535 for an unsigned integer.

For 4 bytes, it’s 0 to 4,29,49,67,296 for an unsigned integer.

Output

3. FLOAT

Floating point numbers are used to store decimal numbers. The range of values it can store also varies from compiler to compiler.

For 32-bit and 64-bit compilers, it is the same as 4 bytes. That is 2^(4*8) length of value, which is 4,29,49,67,296 i.e. 0 to 4,29,49,67,296 numbers can be represented using float.

Output

Size of various data types in my computer(64-bit). Using a similar program, you can find the size in your own compiler. (You do this exercise to clear all your doubts regarding size!!)

Output:

4. VOID

It is a special type known as empty data type that is used to state that a given variable does not have any type. This is mainly used in defining functions where we do not want to return any value.

Following is the data of data type size based on a 32-bit compiler and 64-bit compiler

data type size based on a 32 bit and 64 bit

B. Secondary Data Types

Secondary data types are formed by combining two or more primary data types in C.

They are mainly of two types:

  1. USER-DEFINED DATA TYPES
  2. DERIVED DATA TYPE

1. USER-DEFINED DATA TYPES IN C

These data types are defined by the user as per their convenience. If a user feels a need of having a data type which is not predefined in C library, then they make their own. User-defined c data types

1. STRUCTURE

Structure is a user-defined data type in C, where we can store values of multiple data types.

For example, if you want to store details of students of a college, where each student will be having a name, roll no and marks. But managing the data of one student together (string, int and float variable) for a student is not possible with any of the data types we have discussed so far. Now for this we use another data type known as Structure.

Let's say I create a structure of students with the fields such as name, roll no and marks. Now for each student I can create a variable of structure which will store data of that particular student. And I can access it whenever I want.

Structure data type in C

2. UNION

Union is quite similar to structure as it is also used to store values of multiple data types.

The only difference between structure and union is that, in union the space allocation is equal to the highest memory required by the data type.

Suppose we have two variables, one of type float(4 bytes) and one of type char(1 byte). Now struct will require memory of 5 bytes (4 bytes of float in 1 byte of char) but union will require 4 bytes (4 bytes of float which is maximum among all variables).

difference between structure and union data type

3. TYPEDEF

It is a keyword present in C, which is used to give a new name to any data type.

For example, sometimes typing whole data type name can be tiring, now you can give a short name using typedef i.e, if you want to write unsigned long int in short form as INT, then you can use typedef as:

typedef unsigned long int ul; ul i1, i2;

4. ENUM

Enum is a user-defined data type, which is used to make a program more readable and understandable. It is used to assign text values to integer values.

It basically uses an indexing method and assigns text values to the concerned index value.

In the below given figure 0 will be assigned to Jan, 1 to Feb, 3 to Apr and so on. Enum c data type

OUTPUT:

2. DERIVED DATA TYPES

Derived data types are data types which are formed by combining one or more primitive data types or basic data types in C. For example, there might be some cases where primitive data types are not sufficient for us. Like if we want to store a phone number we can use an integer but what if we want to store phone numbers of all the students in a college. Making variables for each of them is not the optimal way.

To deal with such situations optimally, C has some derived data types, which we can use as per our convenience.

  1. ARRAY
  2. STRING
  3. POINTER
  4. FUNCTIONS

1. ARRAY

Array is a derived data type which is used to store the data of the same type in a contiguous memory location. We can store any type of data types ranging from int , float, double, char to pointer, string, and structure. That is we can make an array of primitive, user-defined or of derived data types.

Array provides random access that is, we can get the value of any element in the array using its index value. Array derived data type in c In the image shown above, you can see how the elements are stored in an array( in a contiguous manner) and how the indexing is done( starts with 0 not 1). That is, the 9th element is accessed using index 8.

For example, if you want to store all of your marks. You can use an integer array. And then, you can get access to your marks by simply using its index. Let us understand it through code. (If you will carefully observe the code, the element at ith index is accessed using arr[i])

Output

We can also make an array of structures.

Input

Output

2. STRING

String is an array of characters. The difference between a normal character array and a string is that string contains ‘\0’ value at the end, indicating that this is the end of the string, while a simple character array does not need to have this.

‘\0’ is a special character, which is known as the NULL character and it is used to indicate the termination of a string.

There are many ways to initialise the string in C. string character in c data type In the image shown above, you can see how the characters are stored internally in a string. Yes, it follows the indexing property of an array, therefore, string is also known as an array of characters. Observe the ‘\0’ (NULL) at the end of the string. It signifies the end of a string.

Output

We can traverse each character of a string using its length or by using ‘\0’. Let us see how:

Output

3. POINTER

A pointer is a special type of data type. It is unique because, unlike other data types, it does not store normal values in variables. Instead, a pointer variable stores the memory address of a specific location in the computer's memory. Pointers are also considered a type of variable and require memory allocation to store their values. The size of the memory allocated to a pointer is determined by the architecture of the computer and the operating system being used. In a 32-bit system, the size of a pointer is typically 4 bytes, while in a 64-bit system, the size of a pointer is typically 8 bytes. It's important to note that the data type of a pointer is always the same as the data type of the variable whose address it is storing.

Now a quick question for you guys: Why do all the pointers have the same size? The answer is hidden in the above discussion. If you did not get it just give it a reread !!

Now we can perform two main operations with the help of pointers. Firstly, we can store the address of a variable, known as initialisation and secondly, we can get value at that address, known as dereferencing. ‘&’ is used for initialisation and ‘*’ is used for dereferencing.

Now let us see how it is actually implemented. Below is a simple code for incrementing the value of an integer using a pointer variable.

You can also understand this concept from the pictorial representation given below. Pointer c data type

4. FUNCTIONS

Functions in C or in any programming language refers to a set of instructions that, when executed in a given order, performs a specific task. The task could be like, finding maximum element, sorting the elements, reversing the elements etc.

Let us consider a scenario where you want to calculate the total marks of all the students. Now writing the code of addition every time for every student is not an optimal way. Does there exist any way by which we just have to write the code of addition once and can use it any time we want?? The answer is YES!!!

To deal with these types of situations, we use a function which performs a desired task for the user ( sum function in this case).

There is a main function present in every C program, all the other functions are declared there only.

Apart from the main function, it is upto to the user how many functions they want to add. functions in c data type

Function Declaration in C

Below is a code of sum of two numbers inputted from the user.:

Output

In a nutshell, data type is used to define the type of data that a variable contains. It can be of primitive (simple) type like integer, character, float or secondary (a little bit complex) like struct, union, array, pointer etc.

We can use any predefined data type as required by our program or we can simply create our own data type (array, struct, union), if we do not find any suitable predefined data type.

Now we have almost discussed all of the data types you will need during your programming journey with C. It might have appeared overwhelming to you to learn about so many data types. But it is very important to know about these data types.

And as you start using them, you will become more comfortable.

Conclusion

In this article, we learnt:

  • The different categories on which we can differentiate data types, i.e. primary and secondary.
  • We understood different primary data types including integer, character, float, void.
  • We also learnt how to use secondary data types like user-defined data types and derived data types.