In the previous post, we had seen what are the various built-in data types in C++. We are now ready to use them to create variables in our programs. First let us see what variables actually are and how they work.

Definition :

Variables are symbolic names given to memory locations that store various values.

In other words, when we declare a variable and assign it any value, that value gets stored in memory at some location. And this value can change – it is not fixed.

VARIABLE DECLARATION

  1. Variables need to be declared before they can be used in a program.
  2. This is done to tell the compiler about the name and type of the variable.
  3. For declaring a variable, we use this syntax: type name ;
  4. ‘type’ refers to the data type the variable will store.
  5. ‘name’ refers to the symbolic name used to denote the variable.
  6. The name can be any valid identifier.

As we have seen in the previous post, we have four basic built-in data types in C++: int, char, float, double. So we can have integer variables, floating-point variables, character variables and double variables.

For example:

  • int num1;
  • float num2;
  • char ch;
  • double num3;

VARIABLE INITIALIZATION

  1. After declaring variables, we need to assign values to them. This is called variable initialization.
  2. Initialization Syntax:  variable_name = value;
  3. ‘=’ is the assignment operator. It assigns the value specified on its right side to the variable name on the left.
  4. value should be of the type we have specified in the variable declaration.

For example: The variables declared above  can be initialized as follows:

  • num1 = 5;
  • num2 = 12.9;
  • ch=’A’;

NOTE: We can also declare and initialize variables in a single statement. For eg:  int number=8;

Let us take up an example program to understand variables better. This program asks the user (student) to enter marks in three subjects. Then it calculates the total marks and percentage of marks.

Here is a live preview of this program along with the output.

Declare and Initialize Variables in C++

Now let us see what is happening in this program.

We have taken 3 integer variables to store marks of three subjects. And we have taken three float variables to store total marks, maximum marks, and percentage.

max_marks which store maximum marks of a student is initialized to value 300; although this variable is storing the value 300 which is an integer, it is automatically converted to float by the compiler.

total_marks and percentage are then calculated.

Notice that both these variables are initialized during program execution, using values of other variables and not with constant values. Even this is possible with variables!

So this is all about variables in C++. Hope you have understood the topic. Do let us know your queries in comments below.



ABOUT THE AUTHOR:

Founder & Senior Editor of ChewCode.com and Editor at GizmoBolt.com. Sakshi is currently pursuing MCA. She is a foodie and has a unique love for learning computer programming languages. Follow her on Google + | Facebook | Twitter. Contact her at [email protected] for feedback and sending tips.


Tags: , , , , ,

Leave A Response