Variables in C Plus Plus (C++):
A variable can be defined as “a quantity that varies during program execution”.
A variable is a symbol that represents a storage location in the computer’s memory. The information that is stored in that location is called the value of the variable. One common way for a variable to obtain a value is by an assignment. This has the syntax:
variable = expression
First, the
expression
is evaluated and then the resulting value is assigned to thevariable
. The equal sign “=” is the assignment operator in C++.
Declarations in C++:
A declaration associates a group of variables with a specific data-type. All variables must be declared before they can appear in executable statements.
A declaration consists of a data-type, followed by one or more variable names, ending with a semicolon.
Example:
Here,
a, b
and c
are declared to be integer variables, root1
and root2
are floating-point variables and flag
is a char-type variables.
These declarations could also have been written as follows:
Initial values can be assigned to variables within a type-declaration. To do so, the declaration must consist of a data-type, followed by a variable name, an equal sign (=) and a constant of the appropriate type. A semicolon must appear at the end, as usual.
Example:
Thus, in above example,
c
is an integer variable whose initial value is 12, star
ia a char-type variable initially assigned the character “*”, sum
is a floating-point variable whose initial value is 0. and factor
is a double-precision variable whose initial value is 0.123458 x 10-6.
0 comments:
Post a Comment