C Language

Integers in C

In C Language by Baqir AliLeave a Comment

Integers in C

Integer is written in C language as Int. Integers are whole numbers, which have no fractional part, or decimal point.

  • They can be both positive and negative.
  • Unsigned integers (positive values only) are supported.
  • In addition, there are short and long integers.
  • Examples of integers include:
    • 0   57   4567   128933
  • The keyword used to define integers is: int
  • Integer variables are declared using:
    • int variable_name;
    • For example: int a;

Long Integers

Sometimes extra digits are needed to store numbers larger than those allowed in a normal int and it is declared as long int variable_name.

The long qualifier informs C that extra storage is required for the integer. Long integers are normally in the range

-231… 231-1 or -2147483648…2147483647

Short Integers

Sometimes less digits are needed to store a number such as

short int variable_name;

The short qualifier informs C that less storage is required for the integer and Short integers are normally in the range:

  -215… 215-1 or -32768..32767

Unsigned Integers

Unsigned integers contain positive values only

  • An integer with n bits uses n bits for the number
  • An integer with n bits → 2n integers in the range [0,2n-1]
    •   e.g. if n=16 → 65536 integers, 0..65535
  • Unsigned integers are declared:
    • unsigned int variable_name;
    •   unsigned short variable_name;
    •   unsigned long variable_name;

Signed Integers

Signed integers allow both positive and negative values

  • An integer with n bits uses n-1 bits for the number and 1 bit for the sign
    • (+ve = 0, -ve = 1)
  • 2n integers in the range [-(2n-1),(2n-1-1)]
    •     e.g. n=16 → 65536 integers, -322768..32767
  • All int declarations default to signed
  • Signed integers are declared:
    • signed int variable_name;
  • which is equivalent to:  int variable_name;

Integers in C On a 16-bit System

Type Approximate Range
short -32768..32767
unsigned short 0..65535
int -32768..32767
unsigned int 0..65535
long -2147483648..2147483647
unsigned long 0..4294967295

A note on Integers/ints

Suffixes can be appended to an integer constant to specify its type. Can be used with int, long, or unsigned long

Example Type Suffix
u or U unsigned 37U
l or L long 37L
ul or UL unsigned long 37UL

Support us by sharing this post

About the Author

Baqir Ali

Facebook Twitter

Hi, I am Baqir Ali ---- Blogger, Programmer, and a Teacher. I am currently working as a Programmer for Softnat Technologies. I have tons of ideas and data to share with you all via blogging.

Leave a Comment