Variables And there Initialization
What are variables????
Variables are memory location that we use in a program to store a value in computer.We can change this value when ever we want...
for example
We want to add two numbers and we also want to take numbers from the user then we must had atleast two variables in program.
Here is the example of the variables in a program
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int x,y;
cout<<"Plz enter First Number ";
cin>>x;
cout<<"Plz Enter second Number ";
cin>>y;
cout<<"The sum of both number is = "<<x+y;
getch();
}
<iosream.h>
is a header file which is used in program for "cout (to show massage or output to user ) " and "cin (to take or input value from user )
<conio.h>
is use for "getch(); .This shall help to avoid instant closing of Program.Program shall remain on screen until you click any key to close it.Int
is a data type.In int data type we can only store integers in it.To know about other Data Types Click Here.The Output of The above program is given below...
Initialization Of Variable
Why we initialize variables????
we initialize a variable because sometimes program enter the garbage value in the program instead of the value given by the user,and this produce wrong RESULT in the end.
Now Lets see how to initialize a value
type identifier = initial_value; For example, to declare a variable of type
int called x and initialize it to a value of zero from the same moment it is declared, we can write:
|
|
A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses (
()):type identifier (initial_value); For example:
|
|
Finally, a third method, known as uniform initialization, similar to the above, but using curly braces (
{}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):type identifier {initial_value}; For example:
|
|
All three ways of initializing variables are valid and equivalent in C++.
|
|

No comments:
Post a Comment