Topic :Variables in C++ Programming Language: Local Variables | Global Variables |

What is a Variable?

A variable is a location in computer memory where We can store and retrieve a value. Our computer’s memory can be thought of as a series of cubbyholes lined up in a long row. Each cubbyhole is numbered sequentially. The number of each cubbyhole is its memory address. variable and address.

Variables have addresses and are given names that describe their purpose. In a simple program, we could create a variable named age to hold the student age and we can assign it value. A variable is a label on a cubbyhole so that it can be accessed without knowing the actual memory address.



computer Memory

Variable naming conventions:

An variables starts with a letter A to Z or a to z. an underscore (_) followed by zero or more letters, underscore, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers.



C++ is a case-sensitive programming language. Thus, "InfoBrother" and "infobrother" are two different variables in C++.


While Mathematicians are content with giving their variables one-letter names like x, Programmers should use longer, more descriptive variable names, such as altitude, sum, User_name are better than the equally permissible a, s and u. A variable's name should be related to its purpose within the program. Good variable names make programs more readable by humans.



  • Rules to create identifiers.

    C++ has strict rules for variable names. We must Know these simple rules before creating any variable in C++ programming.

    » Identifiers must contain at least one character.
    » The first character must be an alphabetic letter (upper or lower case) or the underscore.
    » The remaining character may be alphabetic characters (upper or lower case), the underscore, or a digit.
    » NO other characters (Including spaces) are permitted in identifiers.
    » A reserved word cannot be used as an identifier.
    » Here are some example of acceptable identifiers:



Add , sum , Omar , Move_left , a_001 , totalAge, _temp


Local variables in C++:

Variable defined inside the function is local variables. A local variable occupies memory only when the variable is in scope. When the Program execution leaves the scope of local variables, it frees up the memory for that variable. this freed up memory is then available for use by the local variables in other functions during their invocations.

We can use the single variable name in multiple functions without any conflict. because the compiler derives all of its information about a local variable used within a function from the declaration of that variable in that function. The compiler will not look for the declaration of a local variable in the definition of another function. thus, there is no way a local variable in one function can interfere with a local variable in another function. A local variable has an undefined value after it is declared without being initialized. Its value should not be used until it has been properly assigned.



Global variables in C++:

A variable defined outside of all function is Global variable. it is not local to any particular function. in fact, any function that appears in the text of the source code after the point of the global variable's declaration may legally access and/or Modify that global variable. Global variables, however, do not need to be initialized before they are used. Numeric global variables are automatically assigned the value zero.



Declaring variables in C++:

Before we can use a variable, we need to tell the compiler about it by declaring it (the compiler is very picky about being told about things in advance). To declare a variable we use this syntax.



int x;    /*Data-Type  Variable-name*/
x = 10;   /*Assign value*/


We can assign the Variable, and we can assign value to variable whenever we need it. or we can assign value to variable at the time of declaration as show in the given example.


int x = 10;    /*Data-Type  Variable-name = value*/


We can declare multiple variable of same type at single line using comma operator. we need to Separated each variable name by using comma as shown in the given example:


int x, y, z;    /*Data-Type  Var1, var2, var3 = value*/


Variable Scope:

In a C++ program, variables can be defined either within the program’s functions or outside of them. This has the following effect:
» A variable defined outside of each function is global, i.e. it can be used by all functions.
» A variable defined within a function is local, i.e. it can be used only in that function.



Initialize variables in C++:

A variable can be initialized, i.e. a value can be assigned to the variable during its declaration. Initialization is achieved by placing the following immediately after the name of the variable:

» An equals sign ( = ) and an initial value for the variable as shown in the given example.

int x;   /*Integer Type.*/
 char ch; /*Character type.*/
 
 x = 10;   /*Assign integer value to x.*/
 ch = 'z'; /*Assign Character value to Ch.*/
 
 /********************************************************/
 //or we can assign value at the time of declaration:
 int x = 10;
 char ch = 'z';


» We can initialize the variable using round brackets containing the value of the variable as shown in the given example:

int x(10);


Let't have an simple program where we will use local and Global variables



// Circumference and area of a circle with radius 2.5
#include <iostream>
using namespace std;
const double pi = 3.141593; //Global variable.
 
main()
{
	double area, circuit, radius = 1.5; //local variable.
	area = pi * radius * radius;
	circuit = 2 * pi * radius; 
 
	cout << "To Evaluate a Circle\n" << endl;
 
	cout << "Radius: " << radius << endl
	     << "Circumference: " << circuit << endl
	     << "Area: " << area << endl;
 
	return 0;
}


When a local variable is defined, it is not initialized by the system, we must initialize it by our self. Global variables are initialized by zero automatically by the system when we define it. It is a good programming practice to initialize variables properly, otherwise, sometimes program would produce the unexpected result.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement