C Data types Complete Tutorial

In this section, we will learn what the data types are and how to use them in C.

Note: Before reading this section, we’re expecting you to know about variables. If don’t then please make sure to read the variables section first.

What is Data Type in C? (Variables Type)

The definition of the word `type` according to the Longman dictionary is: one member of a group of people or things that have similar features or qualities.

If you think about it, there are many of these groups around us!

Flowers:

Let’s name five types of flowers:

  1. Rose 2- Daffodil 3- Larkspur 4- Irises 5- Lavender.

Foods:

Now let’s name five types of foods:

1- Vegetable 2- Fruit 3- Grain 4- Seafood 5- Dairy Product.

In our day-to-day life, we face and live with many types like:

  • Types of humans
  • Types of behavior
  • Types of vehicles
  • Types of communications.
  • Types of food
  • Types of headache
  • Types of data

You see, pretty much any programs work with data!

For example, a calculator takes numbers as its data. A video player takes video data as its input. An image viewer, of course, takes image data and so on.

The C language supports a couple of data types and we use them when we want to create a variable or a function.

Before going deep in data-types supported in C, let’s see what the purpose of data-types is in programming languages like C first:

Why there are Data Types in C?

Data-types in short serve two purposes:

  1. Helping compilers to decide the type of value that can be assigned to the target variable or the type of value that returns from a function call:

A character string value like “this is a string of characters”, integer values like 1, 2, 3, 4, float values like 2.3, 4.2, 1000.323, 1.00 etc. all these values are of different types and categories! When we declare the type of a variable, from that moment forward, we can only assign that type of value to the target variable.

For example:

int firstNumber;

The variable named `firstNumber` is of type `int` (which stands for `integer`) and so we can only assign integer numbers (those numbers that don’t have fraction), to this variable.

For example:

firstNumber = 10;

firstNumber = 0;

firstNumber = -44535;

firstNumber = 4223;

Note: the sign `=` is not an equal sign but `assignment` sign. In C programming `==`symbol is an equal sign instead. You can learn more about them in the assignment operators section.

Also, if you try to assign a value of different type than the one set for the target variable, C compilers will try to cast that value to match the variable’s type, which might end up losing a portion of that value. (Check the casting section for more information)

  1. Helping compilers to decide how much space in memory should be set aside for that particular variable:

Let’s say you have 3 pieces of paper. The size of papers is divided into small, medium and large.

Now someone asked you to write down a number and she gives you a clue that the number is in the range of 0 to 10. You decided to pick up the smallest paper because it’s big enough to take the maximum number, which is 10 if that was the case.

Now the same person asks you to write down a larger number. This time she said the range is from one to one trillion.

In this case, you decided to pick up the largest paper you have because there’s a chance that the number end up being one trillion (1 followed by 12 zeros) and so you want to make sure you have enough space for that amount if she asked for this number to be written down.

As mentioned in the variables section, a variable first and foremost should be declared before being used. That means we should set aside some portion of the memory for that particular variable. But how much memory a variable need?

This is where data types can help.

For each data type, compilers set aside a pre-decided amount of space in memory.

For example, `int` data type takes 4 bytes of space in memory. This amount of space is big enough that if we had a variable of this type, we could assign values from -2,147,483,648 to 2,147,483,647.

A `long` is another data type, and it takes 8 bytes of memory space and as a result it’s big enough that a variable of this type can hold values from -9223372036854775808 to 9223372036854775807.

Data types are divided into two groups:

Now that we know what data-types are and why we need them, let’s see the list of data-types that are supported in C language:

Generally , there are two categories of data types supported in C language:

  1. Custom data types: are those types that developers build via enum , union and structure keywords.
  2. Basic data types: these types are built in the core of C language and their size and use cases are already declared.

Primitive Data Types

In this section, we will focus on the basic data types and so the custom data types will be covered in the enum, union and structure sections.

List of basic data types supported in C language:

C int

Integer or `int` is a number with no fraction part. In C, an integer is never written with a decimal point. For example, 1, 200, -300 etc. are integer. Values like 2.3, 4.4, and 543.023 are not.

C int Syntax:

int number;

C int Size:

The memory size of an int variable is at least 4 bytes.

C int Values:

When the type of a variable is `int`, the amount of space that will be assigned to that variable is big enough to hold the range of values from -2,147,483,648 to 2,147,483,647.

C `int` data type Example:

int number = 400;
int number = 3220;

The question is, what will happen if you try to assign a number to a variable larger than the range it can hold?

For example:

#include <stdio.h>

int main() {

    int number = 2147483648;

    printf("result: %d",number);
    return 0;
}
Result: result: -2147483648

You see, in this case, the variable will act like car’s odometer. Which means when the value passed the maximum range of the variable, it’ll start again from the minimum value of that data-type and show the amount of values that surpassed the maximum allowed value.

C short int (or Just short)

The `short int` or simply `short` takes exactly the same type of values that `int` takes with the exception that any variable of this type will have lower amount of memory space comparing to `int` and as a result, lower range of numbers.

C short int Syntax:

short number;
short int number;

C short int Size:

The memory size of a short variable is at least 2 bytes.

C short int Values:

Strictly speaking, a variable of this type can hold the range from -32,768 to 32,767.

C `short` data type Example:

short number = 2000;
short int number = 2000;

Note: There’s no difference between using `short int` or `short`.

C long int (or just long)

The `long int` or simply `long` takes exactly the same type of value that `int` takes with the exception that any variable of this type will have higher amount of memory space comparing to `int` and as a result, higher range of numbers.

C long int Syntax:

long number;
long int number;

C long int Size:

The memory size of a long int variable is at least 8 bytes.

C long int Values:

Strictly speaking, a variable of this type can hold the range from -2,147,483,648 to 2,147,483,647.

C `long` data type Example:

long number = 1000000000;
long int number = 2000000000;

Note: There’s no difference between using `long int` or `long`.

C long long int (or just long long )

The `long long int` or simply `long long` takes exactly the same type of value that `int` takes with the exception that any variable of this type will have a higher amount of memory space comparing to `long int` and as a result, higher range of numbers.

C long long int Syntax:

long long number;
long long int number;

C long long int Size:

The memory size of a long long int variable is at least 8 bytes.

C long long int Values:

Strictly speaking, a variable of this type can hold a range of numbers from –9,223,372,036,854,775,807
to 9,223,372,036,854,775,807.

C `long long` data type Example:

long long number = 1000000000;
long long int number = 2000000000;

Note: There’s no difference between using `long long int` or `long long`.

C char

The `char` type is used for storing characters such as letters, numbers, and punctuation marks, but behind the scene it’s just an integer type. This is because a variable of type `char` stores integers, not characters.

To handle characters, computers use numerical codes in which certain integers represent certain characters.

For example, an integer value 65 represents the character A.

char character ='A';

char character2 =65;

In the example above, even though it seems each variable has different value, behind the scene the real stored value in the memory for both variables will be the binary code of 65.

C char Syntax:

char character

C char Size:

The memory size of a char variable is 1 byte.

C char Notes:

  1. Any character that we assign to this type of variables should be between two single quotation marks `’ ‘` like the way example above showed.
  2. If you decided to use numbers instead of characters as the value of `char` variables, you should know that the range is only from -128 to 127. Also, there’s no need to use a single quotation mark if you’re using the number code of a character. But if you want to store the number itself as a character, then in that case you need to use the single quotations as well.

C unsigned:

All the data types mentioned above can take negative numbers as well.

Basically, they’re `signed` data-type by default.

Note: `signed` data-type refers to those types that accept negative numbers as well.

For example, there’s no difference between:

`signed double = -23.44;`

And:

`double = -23.44;`

Because the default behavior of these data-types is to accept negative numbers as well, we can skip the use of `signed` keyword.

But if we want to have a variable in C to only accept positive values, we can use the unsigned data types.

In short, variables of type unsigned data types won’t accept negative values.

Also on the positive side, the variable of `unsigned data-type` is capable of taking higher range of positive values.

C unsigned data types Syntax:

unsigned data-type-name variable-name

List of Unsigned Data Types

Here’s the list of `unsigned data-types` and the range of values they can take:

Data Type Range
Unsigned char 0-255
unsigned int 0 to 4,294,967,295
unsigned short 0 to 65,535
unsigned long 0 to 4,294,967,295
unsigned long long 0 to 18,446,744,073,709,551,615
unsigned char 0 to 255

Note: the memory size of each `unsigned data-type` is equally the same as what it was without `unsigned`.

C float

For those types of numbers that come with fractions like 1.33, 5.3432, etc. we can use data types like `float`, `double` and `long double`. The difference between each of these data types is the amount of space and accordingly the range of values and precision (for fraction part) they can take.

The first type is `float` data type and, according to the standard C, a float type is able to represent at least six significant figures. For example: 22.6666 or 55.5132 without corrupting or rounding up those fractions.

C float Syntax:

float variable;

C float Size:

The memory size of float type is at least 4 bytes

C float Values:

A float variable is able to represent at least six significant figures.

C `float` data type Example:

float firstNumber = 1.0e3;
float secondNumber = 1000.0;

Both numbers are equally the same. Also, instead of `e` notation, we can also use `E` as well.

C double

Another data-type for holding floating values in C programming is called `double`.

The `double` type doubles the precision range of `float` type! So it means the significant figures are extended to 10 up to 15 (depending on the underlying architecture). For example, 23.12345678 or 2556.65423154, such values are guaranteed to be stored in variables of type `double` without any rounding up.

C double Syntax:

double variable;

C double Size:

The memory size of `double` type is 8 bytes.

C double Values:

A variable of type double can hold values with the significant figures extended to 10 up to 15 (depending on the underlying system)

C `double` data type Example:

double tenFigure = 1.2225467;

C long double

This is the third floating-point data type and its purpose is to provide even more precision than `double`. However, C guarantees only that `long double` is at least as precise as `double`.

C long double Syntax:

long double variable;

C long double Size:

The memory size of `long double` type is 8 or 12 bytes depending on the underlying system.

C `long double` data type Example:

long double number= 2.3334234;

Constant Values

Constant values are those values that are not wrapped by a variable.

For example: 2.3 or 2.555, 2, 34234, 3452342, etc.

The type of these variables depending on their size will be automatically decided by the compiler.

For example, constant fraction values like 2.3, 3.33, etc. are of type `double` by default, but if we want to represent them as `float` type, we need to use the word `f` or `F` after the number. Like: 2.3f or 3432.23423F, etc.

Example: using constant values in C

float number = 43.34533f;
float number2 = 532.334F;

Note that assigning a fractioned number with or without the character `f` to a float type variable doesn’t have any differences, because we already declared the type of variable and so it’ll be float in this example no-matter what.

So is there any use case in using the character `f`?

The answer is definitely yes, as you’ll see in the example below:

#include <stdio.h>

int main() {


    float number= 2.333;

    if (number == 2.333){
        printf("Yes, they are equal \n");
    }else{
        printf("No, they are not equal\n");
    }
    return 0;
}
Result: No, they are not equal

Note: if you’re not familiar with the if-statement, please read the if-else statement section.

In the example above, even though the two numbers are the same or at least this is what it looks like, the result is negative or to the eyes of C language; they are not equal!

This is because the number `2.33` is a constant of type `double` and even though this might not be apparent at first but it took more memory space than the `number` variable of type `float` and as a result there’s no equality between their amount of space in memory.

You can see this yourself by running the example below:

#include <stdio.h>

int main() {


    float number= 2.333;

    if (number < 2.333){
        printf("Yes, number-variable is less than 2.33 \n");
    }else{
        printf("No, number-variable is not less than 2.33\n");
    }
    return 0;
}
Result: Yes, number-variable is less than 2.33

Now if we add the character `f` to the constant value as `2.33f` then in that case the type and values of both variable and the constant value will be equally the same.

#include <stdio.h>

int main() {


    float number= 2.333;

    if (number == 2.333f){
        printf("Yes, they are equal \n");
    }else{
        printf("No, they are not equal \n");
    }
    return 0;
}
Result: Yes, they are equal

Although we mentioned that the size of each basic data-type is fixed, this is actually system dependent. You can learn more about it in the sizeof operator section.

Alright, you might ask why don’t we have just one type for storing all sorts of data in C language.

The answer is the fact that we as developers sometimes work on projects that the memory space is at a premium. In such cases, we need to choose the data types for our variables wisely to save memory space as much as possible. But there are other projects that we have a lot of memory spaces at our disposal and so we can go for those types that allow us to store larger values without concerning on space. That’s why we can’t have just one Data-type in C language.

Note: for integer constant values, the default type is `int`.

For example, if you represent a value like `1000`, this value will be stored in the memory as integer (int) type.

But we can change the type of an integer constant to other types like `long` by adding either `l`or `L` character in front of the value.

For example:

2344L
23L
3342l
4422l
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies