C Integer Data Types

C integer data represents the same as all the mathematical real number to be calculated in the programs. The integer data type is a common feature of almost all the modern programming languages. But the way of representation or declaration of integer numbers may vary language to language.

In this tutorial, we will learn the various methods of integer numbers declarations, calculations and their implementations in the C programming language.

So first of all, we have to keep the integer numbers in the memory address for working with those numbers later in the program. And the way of keeping those numbers in a memory address is declaring the number using a variable. So far we have learned about the variable declaration in a previous lesson.

As C is a statically typed language we have to mention the data type before declaring a variable for integer types. In C, int is the keyword for declaring integer data-container in the memory address.  And the following syntax consists of declaring integers in C.

Example (main.c):

#include <stdio.h>
int main() {
    int a; //declaring single integer without initialization
    int b, c, d, sum; // multiple integers without initialization
    int e=10; //single integer with initialization
    int f=12, g=19; //multiple integers with initialization

    /* initializing value of uninitialized variable */
    a = 14;
    b = 18;
    c = 25;
    d = 36;

    /* re-initializing value of initialized variable
       or changing value of a existing integer value */
    e = 95;


    /* Printing values of integer variables */
    printf("%d \n", a); // printing single variable
    printf("%d %d %d \n", c, d, e); // printing multiple variable

    /* simple arithmetic operations of integers */
    sum = a + b; // sum must have to declare before assaiging values
    printf("%d", sum); // displaying calculated value of sum

    return 0;
}

In the above single program, we tried to show all the basic syntax of declaring integer data type or integer variables. Now if you compile and run the code, you should get the following;

Output:

14
25 36 95
32

In the above output, notice that the third line 32 was the sum variable a and b. We will work with integer numbers with advanced calculations in the later lessons.

C Integer modifiers, size and value ranges

Depending on value ranges and memory allocation size integer type can be modified in many ways in C. Integer data can be stored in the memory address with signed and unsigned format. And both of them have some modifiers for increasing or decreasing memory size like long, short, long long, etc. The following table consists of C integer modifiers:

Data Type Signed Unsigned
Int int or signed int unsigned int
short int or signed short int unsigned short int
long int or signed long int unsigned long int
long long int or signed long long int unsigned long long int

In the above table notice that int and signed int represented as same, and for the remainings are also the same. It is because of all the integer data types are signed by default unless they are declared with the unsigned keyword. So while declaring a signed variable it is not necessary to define the signed keyword.

Each of every single modified integer data types in the above table has a different storage size and value range. For example, short int takes 2 bytes of data from the memory when it is declared. See more at the following table:

Data Types Memory size Min. Value Max. Value
int 2 or 4 bytes -32,768 or -2,147,483,648 32,767 or 2,147,483,647
unsigned int 2 or 4 bytes 0 65,535 or 2,147,483,647
short int 2 bytes -32,768 32,767
unsigned short int 2 bytes 0 65,535
long int 4 bytes -2,147,483,648 2,147,483,647
unsigned long int 4 bytes 0 4,294,967,295
long long int 8 bytes -9,223,372,036, 854,775,808 9,223,372,036, 854,775,807
unsigned long long 8 bytes 0 18,446,744,073, 709,551,615

Using the above data types you can access the corresponding memory size and value ranges in your program.

Placeholder for modifiers

We have learned how to print the value of a variable in the printf function using %d placeholder or format specifier for corresponding variables. But for integer type, all the time the placeholder is not the same. It varies for the modified data types like short int, long int, etc. See the following table for more:

Data Types Placeholder
int %d
short int %hi
unsigned short int %hu
long int %l or %ld or %li
unsigned long int %lu
long long int %lli, %lld
unsigned long long int %llu

Now we will see an example program for testing the above format specifiers are working or not. See the example below.

Example:

#include <stdio.h>
int main() {
    short int a = 35000, b = 3000; //declaring short int type
    long int c = 345; //declaring long int type

    /* short int has the value range of -32,768 to 23767.
    if you declare a higher value in short int than max value
    the execution will be wrong. see output for the below line */
    
    printf("Out of range: %hi \nIn the range: %hi \n", a, b);

    printf("Long value: %ld \n", c); //you can also use %l or %li

    return 0;
}

Output:

Out of range: -30536
In the range: 3000
Long value: 345

Notice that the value of out of range is not showing correctly. It is because of the range limitation of the short int data type. Now you should practice the remaining types randomly on your computer. Happy coding.

« Previous Next »