First Steps in (C) Programming, introduction - Variables That Store Numbers
(Page 3 of 8 )
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 VariablesLet’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, issalary.
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 keywordintto specify that the variable,salary, will be used to store an integer value. The keywordintprecedes 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 variablesalarywill have the value10000. 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 familiarprintf()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 theprintf()function are as follows:
- Argument 1 is 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 variablesalary. 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%dembedded 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 ad, 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.
..........................................................................................
..........................................................................................
Next: Try It Out: Using More 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.
|
|