Sunday, January 8, 2012

The integer types

Every variable in a C++ program must be given atype. The type of a variable is a
specification of the kind of data the variable can store. The most basic type isint.
Anintrepresents an integer quantity. Consider the following program

In this program, two variables, xandy, are declaredto be of type int. These
declarations occur on lines 5 and 6. C++ requires that we specify the type of every
variable, and the declaration statement is the manner by which this is accomplished.
Variables may be declared anywhere in the program so long as they are declared
before they are used.
Subsequently (lines 8 and 9) the variables areassignedvalues. In this casexis
assigned the value 3 andythe value 4. The equal sign =is an operation (called
assignment) that stores the value of the expression to its right into the variable on its
left.
It is possible to combine declarations on a single line; in place of lines 5 and 6, we
could have this:

It is possible to combine declaration with assignment. Lines 5–9 can be replaced
by these two:

It’s easy to see what the rest of Program 2.1 does. In line 11, the contents ofxand
yare added, and the result is written on the computer’s screen.
Running Codes
1 #include
2 using namespace std;
3
4 int main() {
5 int x;
6 int y;
7
8 x = 3;
9 y = 4;
10
11 cout << x+y << endl;
12
13 return 0;
14 }

int x,y;

int x = 3;
int y = 4;

No comments:

Post a Comment