First Steps in (C) Programming, introduction - Try It Out: Using More Variables
(Page 4 of 8 )
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,brothersandbrides, 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 typeintso 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%dconversion specifiers will be replaced by the values currently stored in the variables that appear as the second and third arguments to theprintf()function call—in this case,bridesandbrothers:
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 theprintf()function, so the value ofbridescorresponds to the first specifier, and the value ofbrotherscorresponds 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, theprintf()statement would show clearly which variable corresponded to which conversion specifier, because the output would be
--------------------------------------------
4 brides for 8 brothers
--------------------------------------------
..........................................................................................
Next: Naming Variables >>
More C++ Articles
More By Apress Publishing
|
This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.
|
|