First Steps in Programming - Variables That Store Numbers
(Page 2 of 13 )
There are several different types of variables, and each type of variable is used for storing a particular kind of data. You’ll start by looking at variables that you can use to store numbers. There are actually several ways in which you can store numbers in your program, so let’s start with the simplest.
Integer Variables
Let’s look first at variables that store integers. An integer is any whole number without a decimal point. Examples of integers are as follows:
1
10,999,000,000
–1
You’ll recognize these values as integers, but what I’ve written here isn’t quite correct so far as your program is concerned. You can’t include commas in an integer, so the second value would actually be written in a program as 10999000000.
Here are some examples of numbers that are not integers:
1.234
999.9
2.0
–0.0005
Normally, 2.0 would be described as an integer because it’s a whole number, but as far as your computer is concerned it isn’t because it contains a decimal point. For your program, you must write the integer 2 as 2 with no decimal point. Integers are always written in a C program without a decimal point; if there’s a decimal point, it isn’t recognized an integer. Before I discuss variables in more detail (and believe me, there’s a lot more detail!), let’s look at a simple variable in action in a program, just so you can get a feel for how they’re used.
Try It Out: Using a Variable
Let’s go back to your salary. You can try writing the previous program using a variable:
/* Program 2.2 Using a variable */
#include <stdio.h>
void main()
{
int salary; /* Declare a variable called salary */
salary = 10000; /* A simple arithmetic assignment
statement */
printf(“My salary is %d.”, salary);
}
Type in this example and compile, link, and execute it. You’ll get the following output:
======================================================
My salary is 10000.
======================================================
HOW IT WORKS
The first three lines are exactly the same as in all the previous programs. Let’s look at the new stuff.
The statement that identifies the memory that you’re using to store your salary is as follows:
int salary; /* Declare a variable called salary */
This is called a variable declaration because it declares the name of the variable. The name, in this program, is salary.
CAUTION Notice that the variable declaration ends with a semicolon. If you omit the semicolon, your program will generate an error when you compile it.
The variable declaration also specifies the type of data that the variable will store. You’ve used the keyword int to specify that the variable, salary, will be used to store an integer value. The keyword int precedes the name of the variable. As you’ll see later, declarations for variables that store other kinds of data consist of another keyword specifying a data type followed by a variable name in a similar manner.
NOTE Remember, keywords are special C words that mean something specific to the compiler. You must not use them as variable names or your compiler will get confused.
The next statement is
salary = 10000;
This is a simple arithmetic assignment statement. It takes the value to the right of the equal sign and stores it in the variable on the left of the equal sign.
Here you’re declaring that the variable salary will have the value 10000. You’re storing the value on the right (10000) in the variable on the left (salary). The = symbol is called the assignment operator because it assigns the value on the right to the variable on the left.
You then have the familiar printf() statement, but it’s a little different from how you’ve seen it in action before:
printf("My salary is %d.", salary);
There are now two arguments inside the parentheses, separated by a comma. An argument is an item of data that’s passed to a function. In this program statement, the two arguments to the printf() function are as follows:
- Argument 1is a control string, so called because it controls how the output specified by the following argument (or arguments) is to be presented. This is the character string between the double quotes.
- Argument 2 is the variable salary. How this variable will be displayed is determined by the first argument—the control string.
The control string is fairly similar to the previous example in that it contains some text to be displayed. However, if you look carefully, you’ll see %d embedded in it. This is called a conversion specifier for the variable.
NOTE Conversion specifiers always start with a % character. Because a % in a control string always indicates the start of a conversion specifier, if you want to output a % character you must use the sequence %%.
Conversion specifiers determine how variables are displayed on the screen. In this case, you use a d, which is a decimal specifier that applies to integer values (whole numbers). It just means that the second argument, salary, will be interpreted and output as a decimal (base 10) number.
Try It Out: Using More Variables
Let's try a slightly larger example:
/* Program 2.3 Using more variables */
#include <stdio.h>
void main()
{
int brothers; /* Declare a variable called
brothers */
int brides; /* and a variable called brides
*/
brothers = 7; /* Store 7 in the variable
brothers */
brides = 7; /* Store 7 in the variable brides
*/
/* Display some output */
printf("%d brides for %d brothers", brides, brothers);
}
If you run this program you should get the following output:
======================================================
7 brides for 7 brothers
=====================================================
HOW IT WORKS
This program works in a very similar way to the previous example. You first declare two variables, brothers and brides, with the following statements:
int brothers; /* Declare a variable called brothers */
int brides; /* and a variable called brides */
Both of these variables are declared as type int so they both store integer values. Notice that they’ve been declared in separate statements. Because they’re both of the same type, you could have saved a line of code and declared them together like this:
int brothers, brides;
When you declare several variables in one statement, the variable names following the data type are separated by commas, and the whole line ends with a semicolon. This can be a convenient format, although there’s a downside in that it isn’t so obvious what each variable is for, because if they appear on a single line you can’t add individual comments to describe each variable. However, you could write this single statement spread over two lines:
int brothers, /* Declare a variable called
brothers */
brides; /* and a variable called brides
*/
By spreading the statement out over two lines, you’re able to put the comments back in. The comments will be ignored by the compiler, so it’s still the exact equivalent of the original statement without the comments. Of course, you might as well write it as two statements.
Note that the declarations appear at the beginning of the executable code for the function. You should put all the declarations for variables that you intend to use at the beginning.
The next two statements initialize each of the variables with the same value, 7:
brothers = 7; /* Store 7 in the variable brothers */
brides = 7; /* Store 7 in the variable brides */
Note that the statements that declared these variables precede these statements. If one or the other of the declarations were missing or appeared later in the code, the program wouldn’t compile.
In the next statement is a control string that will display a line of text. Within this text, the %d conversion specifiers will be replaced by the values currently stored in the variables that appear as the second and third arguments to the printf() function call—in this case, brides and brothers:
printf("%d brides for %d brothers", brides, brothers);
The conversion specifiers are replaced in order by the values of the variables that appear as the second and subsequent arguments to the printf() function, so the value of brides corresponds to the first specifier, and the value of brothers corresponds to the second. This would be clearer if you changed the statements that set the values of the variables as follows:
brothers = 8; /* Store 8 in the variable brothers */
brides = 4; /* Store 4 in the variable brides */
In this somewhat dubious scenario, the printf() statement would show clearly which variable corresponded to which conversion specifier, because the output would be
=====================================================
4 brides for 8 brothers
===========================================================
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Naming Variables >>
More Code Examples Articles
More By Apress Publishing