Skip to main content

C++ Variables


Declaration & Assignments
C++ Variables
In C++ variable is used to store data in memory location, which can be modified or used in program during program execution...

Variables play a major role in constructing a program, storing values in memory and dealing with them. Variables are required in various functions of every programs. For example, when we check for conditions to execute a block of statements, variables are required. Again for iterating or repeating a block of statement(s) several times, a counter variable is set along with a condition, or simply if we store the age of an employee, we need an integer type variable. So in every respect, variable is used. In this tutorial, you will learn about how variables are declared in C++, how to assign values in them, and how to use them within a C++ program.

What are Variables?

Variables are used in C++ where you will need to store any type of values within a program and whose value can be changed during the program execution. These variables can be declared in various ways each having different memory requirements and storing capability. Variables are the name of memory locations that are allocated by compilers and the allocation is done based on the data type used for declaring the variable.

Variable Definition in C++

A variable definition means that the programmer writes some instructions to tell the compiler to create the storage in memory location. The syntax for defining variables is:
Syntax:
data_type variable_name;
data_type variable_name, variable_name, variable_name;
Here data_type means the valid C++ data type which includes int, float, double, char, wchar_t, bool and variable list is the lists of variable names to be declared which is separated by commas.
Example:
/* variable definition */
int width, height, age;
char letter;
float area;
double d;

Variable Initialization in C++

Variables are declared in the above example but none of them has been assigned any value. Variables can be initialized and initial value can be assigned along with their declaration.
Syntax:
data_type variable_name = value;
Example:
/* variable definition and initialization */
int width, height=5, age=32;
char letter='A';
float area;
double d;

/* actual initialization */
width
= 10;
area
= 26.5;
There is some rules must be in your knowledge to work with C++ variables.

Rules of Declaring variables in C++

  • Variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
  • The first character must be a letter or underscore.
  • Blank spaces cannot be used in variable names.
  • Special characters like #, $ are not allowed.
  • C++ keywords can not be used as variable names.
  • Variable names are case-sensitive.
  • A variable name can be consisting of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
  • Variable type can be bool, char, int, float, double, void or wchar_t.

Here’s a Program to Show the Usage of Variables in C++

Example:
#include <iostream> 
using namespace std;

int main()
{
int x = 5;
int y = 2;
int Result;
Result = x * y;
cout
<< Result;
}
Another program showing how Global variables are declared and used within a program:
Example:
#include <iostream> 
using namespace std;

// Global Variable declaration:
int x, y;
float f;

int main()
{
// Local variable
int tot;
float f;
x
= 10;
y
= 20;
tot
= x + y;

cout
<< tot;
cout
<< endl;
f
= 70.0 / 3.0;
cout
<< f;
cout
<< endl;
}


Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...