Learn C - C Int Type

Signed Integer Types

We have five basic types of variables to store signed integer values, so positive and negative values can be stored.

Each type is specified by a different keyword or combination of keywords, as shown in the following tables.

Type nameNumber of bytes
signed char1
long long8

Here are some declarations for variables of these types:

short shoe_size; 
int house_number; 
long long star_count;

The type names short, long, and long long can be written as short int, long int, and long long int, and they can optionally have the keyword signed in front.

However, these types are almost always written in their abbreviated forms as shown in the table above.

Type int can also be written as signed int.

The range of values you can store, depends on the particular compiler you're using.


#include <stdio.h> 
int main(void) 
{ //w ww . ja v  a 2 s  . c o  m
      int ten = 10; 
      int two = 2; 
      printf("%d minus %d is %d\n", ten, 2, ten - two ); 
      return 0; 

The code above generates the following result.

Unsigned Integer Types

Some data are always positive.

For those data you don't need to provide for negative values.

For each signed integers, there is a corresponding unsigned type integers, and the unsigned type occupies the same amount of memory as the signed type.

Each unsigned type name is the signed type name prefixed with the keyword unsigned.

The following table shows the basic set of unsigned integer types that you can use.

Type nameNumber of bytes
unsigned char1
unsigned short or unsigned short int2
unsigned int4
unsigned long or unsigned long int4
unsigned long long or unsigned long long int8

With a given number of bits, the number of different values that can be represented is fixed.

For example, a 32-bit integer variable can store 4,294,967,296 different values.

Using an unsigned type doesn't provide more values than the corresponding signed type.

Here are examples of unsigned integer variable declarations:

unsigned int count; 
unsigned long population; 

The following code shows how to declare an unsigned int.

#include <stdio.h> 
int main(void) 
   unsigned int iResponse = 0; 
   printf("%d", iResponse); 
   return 0;

The code above generates the following result.

Integer Constants

Because you can have different types of integer variables, we have different kinds of integer constants.

If you just write the integer value 100, for example, this will be of type int.

If you want to make sure it is type long, you must append an uppercase or lowercase letter L to the numeric value.

So 100 as a long value is written as 100L.

To declare and initialize the variable my_value, you could write this:

long my_value = 123456789L;

You write negative integer constants with a minus sign, for example:

int decrease = -4; 
long  my_value = -100000L;

You specify integer constants to be of type long long by appending two Ls:

long long my_value = 123456789LL;

To specify a constant to be of an unsigned type, you append a U, as in these examples:

unsigned int count = 100U; 
unsigned long value = 999999999UL; 

To store integers with the largest magnitude, you could define a variable like this:

unsigned long long my_value = 9876543212345678ULL;

The ULL specifies that the initial value is type unsigned long long.

Hexadecimal Constants

You can write integer values in hexadecimal form which is base 16.

The digits in a hexadecimal number are the equivalent of decimal values 0 to 15, and they're represented by 0 through 9 and A though F (or a through f).

Hexadecimal numbers are written with the prefix 0x or 0X.

Hexadecimal constants can have a suffix.

Here are some examples of hexadecimal constants:


Octal Constants

An octal value is a number to base 8.

Each octal digit has a value from 0 to 7, which corresponds to three bits in binary.

An integer constant that starts with a zero, such as 014, will be interpreted as an octal number.

014 is the octal equivalent of the decimal value 12.

The following code prints 100 in decimal, octal, and hex.

#include <stdio.h> 
int main(void) 
{ /*from   w w w.j  a  v a  2  s  .  c  o m*/
    int x = 100; 
    printf("dec = %d; octal = %o; hex = %x\n", x, x, x); 
    printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x); 
    return 0; 

The code above generates the following result.

Integer Overflow

What happens if an integer tries to get too big for its type?

Let's set an integer to its largest possible value, add to it.

Try both signed and unsigned types.

The printf() function uses the %u specifier to display unsigned int values.

#include <stdio.h> 
#define PAGES 959 /*from   w ww .  jav a 2 s .c  om*/
int main(void) { 
   int i = 2147483647; 
   unsigned int j = 4294967295; 
   printf("%d %d %d\n", i, i+1, i+2); 
   printf("%u %u %u\n", j, j+1, j+2); 

   printf("*%d*\n", PAGES); 
   printf("*%2d*\n", PAGES); 
   printf("*%10d*\n", PAGES); 
   printf("*%-10d*\n", PAGES); 
   return 0; 

The integer unsigned int variable j , like an odometer, begins at 0, but the int variable i begins at -2147483648.

The code above generates the following result.

Example 2

The following code uses the portable names for integer types.

#include <stdio.h> 
#include <inttypes.h> // supports portable types 
int main(void) 
  { /*  w  w  w .ja  v  a2s  .c o  m*/
      int32_t me32;     // me32 a 32-bit signed variable 
      me32 = 45933945; 
      printf("First, assume int32_t is int: "); 
      printf("me32 = %d\n", me32); 
      printf("Next, let's not make any assumptions.\n"); 
      printf("Instead, use a \"macro\" from inttypes.h: "); 
      printf("me32 = %" PRId32 "\n", me32); 
      return 0; 

The code above generates the following result.