Pin Me

C Programming For Beginners - Integer Data Types

written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 12/24/2008

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.

  • slide 1 of 4

    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.

  • slide 2 of 4

    Whole numbers - int

    An int variable is used to store an integer (whole number) value. An integer value is one of 0, ~+mn~1, ~+mn~2, ~+mn~3, ~+mn~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.

    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.

  • slide 3 of 4

    Declaring variables

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

    int j;

    declares j to be a variable of type int.

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

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

    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:

    int j = 14;

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

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