Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize also the number 2 at the same time. You have just stored two different values in your memory.
The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in C++ with the following instruction set:
a = 5;
b = 2;
a = a + 1;
result = a - b;
Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.
Declaration of variables:
In order to use a variable in C++, we must first declare it specifying which data type we want it to e. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example:
int a =0 ;
double val = 0.0;
These are two valid declaration and initialization of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier val. Once declared, the variables a and mynumber can be used within the rest of their scope in the program.
If you are going to declare more than one variable of the same type, you can declare all of them in a single
statement by separating their identifiers with commas. For example:
These are two valid declaration and initialization of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier val. Once declared, the variables a and mynumber can be used within the rest of their scope in the program.
If you are going to declare more than one variable of the same type, you can declare all of them in a single
statement by separating their identifiers with commas. For example:
int a, b, c, asl, pet;
This declares three variables (a, b and c ----), all of them of type int, and has exactly the same meaning as:
int a;
int b;
int c;
int asl;
int pet;
Local and Global Variable:
Variables declared outside of a block (outside the main function) are called global variables. Global variables have program scope, which means they can be accessed everywhere in the program.A variable declared inside the a function is called local variable.
A local variable is local in the sense that,when the operating point or control moves out of teh function at the end of it, the variable become undeclared.
Here is an example of declaring a global variable and local variable.
#include<iostream> #include<stdio.h> |
including namepace std;
int global =5; // declaration of global variable
double myfunc(double x, double y)
{
double sum=0.0; // here sum is the local variable
int i=0; //declaration of local variable
}
int main()
{
int local=6; //declaration of local variable
Defined constants ( # define)
You can define your own names for constants that you use very often without having to resort to memory-consuming variables, simply by using the#define
preprocessor directive. Its format is:
#define PI 3.14159 |
This defines two new constants: PI .Once this defined, you can use them in the rest of the code as if they were any other regular constant, for example:
// defined constants: calculate circumference #include <iostream> using namespace std; #define PI 3.14159 int main () { double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle <<endl; return 0; } |
The output is : 31.4159
Declared constants (const)
With theconst
prefix you can declare constants with a specific type in the same way as you would do with a variable: const float PI = 3.14159;
Here, PI is a constants. They are treated just like regular variables except that
their values cannot be modified after their definition.