# The C int and its modifiers – signed, unsigned, short, long

The C int keyword declares an integer value. We use it to create variables, specify function return type or cast to int.

#### Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

## Usage

Creating an integer variable:
int
number = 5;

Specifying a function return type:
int
sum()
{

int result;

...

return result;
}

Casting compatible types to use them as an int. For instance you can print the integer part from a double value:
double
price = 19.50;
printf("%d\n", (int)price);

## Size

The size of the int in C is not fixed. Instead the standard requires that it can hold at least the range from -32767 to 32767. This means the size of the int type is at least 16 bits (2 bytes).

In practice its size depends on the compiler and the machine. Most of the compilers use a 16 bit int for 16 bit (and 8 bit) machines and 32 bit for the rest. Other compilers will match the 16 bit requirement for the older machines and use 32b, 64 etc. for the new 32, 64 bit machines.

The only way to be sure what is the size of the int in your program, use sizeof(int);

Note that it will return the result in bytes (2-4-8).

## C int - modifiers

We can use several modifiers with the numeric types. In C, by default all data is int. If we omit the data type and place only the modifier, it is assumed to be int.

#### Sign modifiers

These modifiers change the interpretation for the most significant bit(MSB).

• signed – the MSB tells if the number is positive(0) or negative(1). By default int is signed, so you will rarely use this combination. The following declarations are identical:

int
number;
signed int number;
signed
number;

• unsigned – the MSB is interpreted as part of the value, just like the rest of the bits. This allows the types to hold twice bigger positive numbers, but we cannot save negative values. The following declarations are identical:

unsigned
number;
unsigned
int number;

#### Size modifiers

The size modifiers are short and long. They may change the size. I say “may”, because their size is not fixed, too. The standard says that long is at least the size of the int, the int is at least the size of short.

• short – in theory its size depends on the implementation. In practice it will be 16 bits on all systems with the most popular compilers. The following declarations are identical:

short
number;
short
int number;
signed
short number;
signed
short int number;

• long – The long modifier can be used with integer and double. Its size, once again, depends on the system. In practice:

- on Windows systems long will be 32bits(4 bytes)
- on pretty much any other system, long takes 64bits(8 bytes)

Here are several equivalent examples:

long
number;
long
int number;
signed long number;
signed
long int number;

Since C99 a new modifier exists – long long. Normally it holds at least 64bits. Here are examples for declaring variables with this type:

long long number;
signed long long number2;
long long int number3;

Because of the uncertainty of the int sizes in C, the new standard defined a set of new types and values in <stdint.h>.
The once that guarantee the data size are:
int8_t
int16_t
int32_t
int64_t

## Summary

The C int type is the natural way to work with integer numbers. Originally it was designed to match the word size of the platform. Its size varies, depending on the compiler and the machine. We can use different modifiers to make it signed, unsigned or increase/decrease its range with short and long.

If you need to know the exact size of the int in a program, use sizeof(int);

The C99 standard adds new types with fixed size and adds the long long modifier.

-    float
-    double

 Previous keyword: if Next keyword: register

#### Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

› int
 Did this help? Support me with your vote ;-)
 Did this help?