Advertisement
Tech

How to Program in C for the Beginner: The Integer Type - int

In this article, we begin to look a little deeper into the basic data types in C. We discuss the integer type, int, and show how to declare variables.

By Noel Kalicharan
Desk Tech
Reading time 3 min read
Word count 612
Linux Computing Linux commands
How to Program in C for the Beginner: The Integer Type -  int
Advertisement
Quick Take

In this article, we begin to look a little deeper into the basic data types in C. We discuss the integer type, int, and show how to declare variables.

On this page

Basic data types

In this article , we briefly touched on the concept of a data type. For most of this series, we will use the following data types: int, double and char. These, among others, are referred to as primitive data types.

Each data type defines constants of that type. When we declare a variable to be of a particular type, we are really saying what kind of constants (values) can be stored in that variable. For example, if we declare the variable num to be int, we are saying that the value of num at any time can be an integer constant such as 25, -369 or 1024.

Advertisement

Whole numbers - int

An int variable is used to store an integer (whole number) value. An integer value is one of 0, ±1, ±2, ±3, ±4, etc. However, on a computer, the largest and smallest integers which can be stored are determined by the number of bits used to store an integer.

Typically, an int variable occupies 16 bits (2 bytes) and can be used to store whole numbers in the range -32,768 to +32,767. Note, however, that on some machines, an int could occupy 32 bits, in which case it can store whole numbers from -2,147,483,648 to +2,147,483,647. In general, if n bits are used to store an int, the range of numbers which can be stored is -2(n-1) to +2(n-1) - 1.

Advertisement

Integer constants can be written in various ways. The most common form is the one we are all accustomed to, using decimal digits, for example, 354, -1, 32905 and 987654321. Note that you are allowed to use only a possible sign (+ or -) followed by digits 0 to 9. In particular, you cannot use commas as you might do to separate thousands; thus 1,713 is an invalid integer constant—you must write it as 1713.

Declaring variables

In C, a variable is declared by specifying a type name followed by the variable. For example,

Advertisement

int j;

declares j to be a variable of type int.

Advertisement

You can declare several variables of the same type in one statement as in:

int a, b, c; // declares 3 variables of type int

Advertisement

The variables are separated by commas, with a semicolon after the last one.

You can declare a variable and give it an initial value in one statement, as in:

Advertisement

int j = 14;

This declares j to be int and gives it a value of 14.

Advertisement

In the next article, we will show how to print an integer using a “field width”.

References

References: C Programming – A Beginner’s Course

Advertisement

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

Advertisement
  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement