Himanshu

Negi

Developer & Internet Marketer


Welcome to my blog.

Sed sed leo sit amet elit sagittis fermentum. Nulla posuere commodo erat. Nam ac nunc ipsum, id espara tincidunt orci. Quisque adipiscing molestie dolor, ut congue sem porta et. Sed sapien urna, auctor et accumsan ut, accumsan sit amet dui. Nunc tristique interdum magna, dictum volutpat sem. Nam ac nunc ipsum, id espara tincidunt orci. Sed sapien urna, auctor et accumsan ut.

Variable in C++ Programming: Create and Perform Operation on Variables

April 29, 2017wp-himu0 Comments

A variable in C++ Programming is a name for a place in the computer’s memory where some value or data can be stored for future use. Get it? In simple words, you can compare a variable to a box (memory box) in which you can store some data (number or characters). Your can create millions of identical boxes (i.e. variables) and therefore it’s necessary to uniquely name each of them so that we can later identify and refer to them appropriately.

For further disquisition, we can categories these boxes/variables  according to the data they have stored. According to their types we can store different types of numbers (integers, floating etc.) or alphabets (letters, symbols and numbers) in our variable.

In C++ we have int (used for integer), float (for floating numbers, decimal), char (represents character) and Boolean (for true or false). In this article we’ll learn about C++ programming variable, rules for creating variables in C++ and mathematical operations on these variables.

How to Create a Variable in C++ Program?

To create or declare a variable in C++ Programming you have to first decide its type (i.e. data type) and then its name. To declare a variable of integer type with the name numsimply write int num; Take a look at the program following C++ program of declaring variable.
[codesyntax lang=”cpp” title=”declaring variable in C++” bookmarkname=”declaring variable in C++”]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
       int num;           // variable declaration 
       num=2;            // value assigned to integer 
       cout<<num; 
       getch(); 
}

[/codesyntax]

In the above C++ program we have declared a variable num of integer type. We cannot store decimal number or fractional number (e.g. 2.3 or 5.02) in integer variable. The range of integer type variable is -32,676 to +32,678. You can’t store a number greater or smaller than that range. If you did that it will store a garbage value that could be anything.
[codesyntax lang=”cpp” title=”declaring variable in C++” bookmarkname=”declaring variable in C++”]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
        float num; 
        num=2.05; 
        cout<<num; 
        getch(); 
}

[/codesyntax]

Here num is the name of a floating variable and we can store integers as well as floating numbers in it (but integer will surely get converted to a floating type).

Rules for Creating Variables in C++ Programming:

  • The first character of variable name must be an alphabetic or an underscore. Eg. int 1num is invaid.
  • Blank spaces are not allowed in a variable name. eg. int my num is invalid.
  • Special characters such as arithmetic operators, #, ^ can’t be used in a variable name. eg. int my#num is invalid.
  • Reserved words (Keywords) can’t be used as variable names. Eg. int cout or int float are invalid.
  • A variable name declared for one data type cannot be used to declare another data type. Int num and float num can’t be defined in same function.
  • C++ language is a case sensitive language. Eg. int num and int Num are different variables.

 

Operation on C++ Variables:

When I say operation on variables I mean, arithmetic operations. We can perform all arithmetic operation on numerical variables that we have learned in mathematics so far (during school days). We can add two or more variable, subtract them and can do anything. Look at the C++ program below.
[codesyntax lang=”cpp” title=”Addition of two variables” bookmarkname=”Addition of two variables”]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
       int variable=5; 
       int other_variable=3; 
       int sum; sum=variable+other_variable;
       Cout<<sum; 
       getch(); 
}

[/codesyntax]

In the above program we had implicitly initialized variables with some initial value. Also, take a look at the following program where we will do addition of three variables.
[codesyntax lang=”cpp” title=”Addition of three variables” bookmarkname=”Addition of three variables”]

#include<iostream.h> ]
#include<conio.h>

Void main() 
{ 
      int var1=50; 
      int var2=20; 
      int var3=55; 

      int sum=var1+var2+var3; 
      cout<<sum; 
      getch(); 
}

[/codesyntax]

To perform other operations, just replace the + sign with required sign (operator).

Facts about C++ Variables

  • Variable holds data temporarily and gets deleted after its scope ends.
  • Values in variables can be overwritten.

Leave a Reply
www.edatastyle.com www.introducingsomething.com