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.
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.
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.
In C, a variable is declared by specifying a type name followed by the variable. For example,
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”.
References: C Programming – A Beginner’s Course
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.
- C Programming For Beginners - Part 1
- C Programming For Beginners - Part 2
- C Programming for Beginners – Part 3
- C Programming for Beginners – Part 4
- C Programming For Beginners - Part 5
- C Programming For Beginners - Part 6
- C Programming For Beginners - Data Types
- C Programming For Beginners - Part 8
- C Programming For Beginners - Part 9
- C Programming For Beginners - Part 10
- C Programming For Beginners - Part 11
- C Programming For Beginners - Part 12
- C Programming For Beginners - Part 13
- C Programming For Beginners - Part 14
- C Programming For Beginners - Integer Data Types
- C Programming for Beginners - Part 16
- C Programming For Beginners - Integer Expressions, Operators and Precedence
- C Programming For Beginners - Part 18
- C Programming For Beginners - Printing Double and Float
- C Programming For Beginners - Mixing double, float and int