Understand C Datatypes

In this C programming class, we’ll cover all available C datatypes, explain their purpose and the limits. You’ll see flowcharts and code examples to understand better.

The data types are the core building blocks in C. Hence, please read with full focus and concentration.

Table of Content

C Datatypes – Let’s Understand from Scratch

1. What are Datatypes in C Programming?

In C programming, data types are just the same what their name suggests. They represent the kind of data to store. They are used to declare several functions as well as variables in a program.

There are mainly three categories of data types, Basic/Primitive, Derived, User-Defined.

This diagram would help you.

C Datatypes to Use in Programs

C Datatypes to Use in Programs

 

Now, we will see what each of them means individually.

1.1. Int

It is used to store the integer data type.

The regular integer that we use has a size of 2 bytes (16 bits) on a 16-bit machine. However, most of the modern systems have 32 or 64-bit configurations. The size of an integer in such environment is 4 bytes with a starting value of -2,147,483,648 and an ending value as 2,147,483,647. Any operations involving a number out of this range will return either garbage or an error.

Note: Garbage value is nothing but waste or unused data in a program’s memory. It could also be related to variables which are no longer in use. So, it is always best to initialize to avoid any undefined behavior.

1.2. Unsigned int

An integer could also be of an unsigned type. Such a type (unsigned int) on 32-bit will have a range from 0 to 4,294,967,295 and 0 to 65535 on a 16-bit system.

1.3. Long

It also stores the integer data with a higher range. The long type is 4 bytes on 32-bit systems and of 8 bytes in 64-bit setups.

The 32-bit long range is same as an integer while on 64-bit, it can hold a minimum of -9,223,372,036,854,775,808 and goes up to 9,223,372,036,854,775,807.

1.4. Unsigned long

In some case, we require only positive numeric values; then it’s better to use the unsigned type. It not only makes sure that you get one but also enhances the range.

The unsigned long is of 4 bytes and ranges from 0 to 4,294,967,295.

1.5. Float

This datatype is also numeric but allows numbers with decimal places. It has a size of 4 bytes and has a range of 3.4E-38 to 3.4E+38.

The float type has a precision up to 7 digits.

1.6. Double

It is same as float but with a higher precision range that is 8 bytes which gives it an ability to store more numerals after the decimal places, double to be exact.

It has a length of 8 bytes and has a range of 1.7E +/- 308. The double data type has a precision up to 15 digits.

1.7. Char

Char data type by default allows a single character such as a letter from the alphabet. It has the size of just 1 byte with a range of -128 to 127 or 0 to 255.

However, with an array [] suffix, it can hold string values, a combination of alpha-numeric characters.

1.8. Unsigned char

Also, there is an unsigned char type which is similar but doesn’t allow the (-ve) values. It has the same size of 1 byte with a range of 0 to 255.

1.9. Short

The length of the short type is two bytes. It allows the range of values equal to 2 to the power 16, i.e., up to 16 bits. Since it is a signed one, hence, contains both the positive and negative values.

It has a range of values between -32768 and 32767.

1.10. Unsigned short

The unsigned short type contains only +ve numbers and also has a size of 2 bytes. It can store a minimum value of 0 and goes up to a maximum of 65 535.

1.11. Array

A C array is a contiguous sequence of elements of a similar data type. It is primarily to create a collection of the primitives like the numbers, chars, struct, and unions.

We have a dedicated chapter for the Arrays which you can go through later.

1.12. Pointers

Pointers are used to point and access the values stored at an address in the program’s dynamic memory known as the heap.

The pointer type variables get stored on the heap while the other types on the stack.

Like an array can have multiple elements, the pointer too can hold several values depending upon the amount of memory it gets allocated.

This data type also has a chapter dedicated to it so we will see more of it later.

1.13. Enum

The enum is used to assign a name to a variable to a constant. We will also are this in further chapters.

1.14. Structure (struct)

The structure type is used to create a data type that can hold multiple data types in it.

1.15. Union

A union is a data type which has all values under it stored at a single address.

In the below C datatypes example, we’ll see the use of the two fundamental C datatypes.

2. Data Types Range and Sizes

We’ve captured all the fundamental C datatypes along with their size in bytes and the range of values in the below table.

C Datatypes Range and Sizes

C Datatypes Range and Sizes

2. Sample Program: Print Quotient of Two Numbers

In this example, we will perform the arithmetic division operation on two integer numbers. After that, the same result will get assigned to two variables of the following types.

  • Integer
  • Float

Let’s first understand the flow of this program with the help of its flowchart.

2.1. Flowchart:

Get Quotient of Two Integers - Flowchart

 

2.2. Algorithm:

Step 1: Start.

Step 2: Initialize variables.

Step 3: Take input from the user.

Step 4: Get the quotient c=d=a/b

Step 5: Print output of the integer as well as the float.

Step 6: Stop.

2.3. Code:

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, c;
    float d;

    printf("Enter two numbers to divide : \n");
    scanf("%d %d", &a, &b);

    c = a/b;
    d = a/b;

    printf("Quotient of %d and %d in the form of int is %d",a , b, c);
    printf("\nQuotient of %d and %d in the form of float is %f",a ,b ,d);

    getch();
}

Note: float data type is declared in printf using %f just as the int is declared using %d.

The output should look something like this.

C Datatypes Program Output

C Datatypes Program Output

 

Note: If we use the double type, then it would also give the same output, but it would matter for programs where accuracy is needed.

We have just covered the basics here. You shall learn more about the data types and their advanced usage in the next C programming tutorials.