Lesson 2.2. Integers

Integer type

The integer type is to store integers. There are 4 types used to encode integers in C. Depending on the size of the values to be stored, you have to choose the appropriate type :

Type Bytes Bits Minimum Maximum
char 1 8 -128 +127
short int 2 16 -32 768 +32 767
int 4 32 -2 147 483 648 +2 147 483 647
long int 8 64 -9 223 372 036 854 775 808 +9 223 372 036 854 775 807

For types short int and long int, there is a short writing: short and long :

short Var;
long Var = 2;

It is important to choose the best type for each variable to save memory. For example, if you use a long to store a number between 1 and 10, you will occupy 4 bytes when only one would be enough.

The size of the types may vary from one compiler to another.

Depending on the compiler and/or the target, the size of the types may vary. For example, if we compiled for an Arduino, an integer will be stored on 2 bytes. It is possible to know the size of a type with the sizeof() function. We will see an example below.

Unsigned

When types are preceded by the keyword unsigned, this specifies that the variable will be un-signed. In other words, it will not have a sign (+ or -). The value contained in the variable is necessarily positive:

Type Bytes Bits Minimum Maximum
unsigned char 1 8 0 255
unsigned short int 2 16 0 65535
unsigned int 4 32 0 4294967295
unsigned long int 8 64 0 +18 446 744 073 709 551 615

Initialization

Integers can obviously be initialized with a value expressed in decimal base:

int x = 12;

It is also possible to initialize integers with a hexadecimal value using the 0x syntax:

// The integer x is initialized with the hexadecimal value C ( 0x0C = 12 in base 10)
int x = 0xC;

On the notation of hexadecimal values, C is not case sensitive (upper or lower case are allowed):

int var = 0xA12F;

is identical to:

int var = 0xa12f;

In standard C, it is not possible to initialize a variable directly in base 2. However, some compilers accept the 0b notation, generally compilers dedicated to to embedded systems.

// This line does not meet the standards of C
// and is not supported by many compilers
PORTA = 0b10110110;

To ensure code compatibility, it is best to avoid this notation. The best solution is to initialize in hexadecimal with an appropriate comment as in this example:

//  |  b7 |  b6 |  b5 |  b4 |  b3 |  b2 |  b1 |  b0 |
//  |  1  |  0  |  1  |  1  |  0  |  1  |  1  |  0  | 
PORTA = 0xB6;

Display

When displaying a variable with the printf function, you have to use a format code. This is a special sequence of characters which tells the function that this sequence must be replaced by the value of the variable.

For example, to display the N integer variable , we use the %d format code:

int N=12;
printf ("%d", N);

The following table presents the format codec for each type:

Type Format code
char %d
unsigned char %d ou %u
short %i
unsigned short %u
int %d ou %i
unsigned int %u
long %ld ou %li
unsigned long %lu

Here is an example that display a long int (10e10 = \( 10^{10} \) ) :

unsigned long N=10e10;
printf ("%lu", N);

Exercises

Exercise 1

The code below displays the size of a char and a short. To retrieve the size of a type, we use the sizeof() instruction which returns its size in bytes. Note that sizeof() returns a value comparable to an unsigned long int, which explains the format code %lu in the printf. Complete the code so that the program also displays the size of an int and a long.

#include <stdio.h>

int main(void) {

    // Size in bytes :
    printf("Size of a char = %lu bytes\n", sizeof(char));
    printf("Size of a short = %lu bytes\n", sizeof(short));

    // COMPLETE HERE

    return 0;
}

Exercise 2

The code below displays the size of a char in bits. Complete the code below so that it also displays the size of an int and a long.

#include <stdio.h>

int main(void) {
    // Size in bits:
    printf("Size of a char = %lu bits\n", sizeof(char)*8 );

    // COMPLETE HERE

    return 0;
}

Exercise 3

The code below is provided:

#include <stdio.h>

int main(void) {

  // Declaration and assignment of the variable a 
  char a=100;  

  // Add 1 to the variable a
  a = a + 1;

  // Display the final value of a
  printf("100 + 1  = %d", a);    

  return 0;
}
  1. Analyse and run the code
  2. Replace the first line with char a=127;.
  3. Run the program, what happens?
  4. Change the type of a to solve the issue

Exercise 4

Write a program that:

#include <stdio.h>

int main(void) {
    // Declaration and assignment of nb_student

    // COMPLETE HERE

    // Display the variable

    // COMPLETE HERE

    return 0;
}

Quiz

To store a value in the range [-100 ; +100], which type is the most suitable?

Check Bravo! Type char encode integers between -128 and +127, that's enough. Try again...

Which type is best suited to memorize a 4-digit credit card code?

Check Bravo! The code will be between 0 and 9999, a short is between -32768 and +32767. Try again...

Which type is best for string a person's age?

Check Bravo! Below is Jeanne Calment, French supercentenarian and the oldest human.
Jeanne Calment, the French doyenne of humanity.

She died at the age of 122. It is not impossible that we will soon reach 128 years. It is better to take an unsigned char that will not occupy more space in memory.
Try again...

Is the size of an int 4 bytes?

Check Bravo! The size of the types can vary from one compiler to another. Try again...

What is a signed number?

Check Bravo! A signed number has a sign + or -. Try again...

What is the largest value that the variable i will hold?

unsigned char i;
Check Bravo! An unsigned char can store values between 0 and 255 (255 = 28 - 1 because of the zero). Try again...

See also


Last update : 11/21/2022