Skip to content
Related Articles

Related Articles

int keyword in C

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 15 Jun, 2022
View Discussion
Improve Article
Save Article

In the C programming language, the keyword ‘int’ is used in a type declaration to give a variable an integer type. However, the fact that the type represents integers does not mean it can represent all integers. The size of an int variable is fixed and determined by the C implementation you use. The C Standard dictates that an int must have a range of at least -32768 to +32767. The C implementation can, and very often do, have a much larger range than this. The range of the int type on a particular C implementation can be obtained via the INT_MAX and INT_MIN variables defined in the header <limits.h>:

C




#include <limits.h>
#include <stdio.h>
 
int main()
{
    printf("minimum int value = %d\n"
           "maximum int value = %d\n"
           "size of int in bytes = %zu\n"
           "size of int in bits = %zu",
           INT_MIN, INT_MAX, sizeof(int),
           sizeof(int) * CHAR_BIT);
}

Output

minimum int value = -2147483648
maximum int value = 2147483647
size of int in bytes = 4
size of int in bits = 32

Do keep in mind though that not all C implementations are the same, and they don’t all have the same ranges for integer types.

SIGNED INTEGER

The int type in C is a signed integer, which means it can represent both negative and positive numbers. This is in contrast to an unsigned integer (which can be used by declaring a variable unsigned int), which can only represent positive numbers.

Attempting to assign a signed integer type with a value that is outside of its range of representable values (from INT_MIN to INT_MAX) will result in undefined behavior.

Signed integers commonly use two’s complement on all modern machines, and in the upcoming (as of Jan 2022) C23 standard, two’s complement is the only valid way of representing signed integers. You can read more about how signed numbers are represented in binary and why two’s complement is the most common representation in this article.

UNSIGNED INTEGER

In the case of an unsigned integer, only positive numbers can be stored. In this data type, all of the bits in the integer are used to store a positive value, rather than having some reserved for sign information. This means the magnitude of the maximum representable value in an unsigned integer is greater than that of a signed integer with the same number of bits.

In the case of an unsigned int value, the maximum value is specified by the UINT_MAX macro, defined in <limits.h>, and the minimum value is 0. As per the C standard, unsigned arithmetic can never overflow, rather it is performed modulo the maximum value of the unsigned type + 1, so for unsigned int, arithmetic is performed modulo UINT_MAX + 1 such that UINT_MAX + 1 is 0, and UINT_MAX + 2 is 1, and so on:

C




#include <limits.h>
#include <stdio.h>
 
int main()
{
    printf("UINT_MAX + 1 = %u", UINT_MAX + 1);
}

Output

UINT_MAX + 1 = 0

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!