SEARCH THIS BLOG

VARIABLES IN C/C++

A programmer writes a code to perform a task. He is responsible for creating all logics and writing the entire code to serve the purpose. But what is the need of a program?

A program is useful when it takes input from users all across the globe and then process it to yield the desired result. This work of taking inputs from users and forming meaningful expressions is done by variables.

 

VARIABLES

Variables (also called Symbolic Variables) represent named storage locations whose values can be manipulated during program run.

A variable is a named memory location which stores values, one at a time, and these values can be changed during the execution of program.

Computer memory consists of memory blocks of 1 byte size, each of which has a unique address. It is through this address that the compiler knows where a particular variable is stored.

Every variable has two values associated with it :

Data value  : It is the actual value stored into the variable and is used for all general calculations and operations. It is referred to as rvalue (read-value)(pronounced as “are-value”).

Location value : It is in a way the location value of a variable. In C++, a lvalue is something that points at a legal memory block. It appears to the left side of assignment operator (=) and is used to assign a rvalue to a variable. This concept is a bit tricky and we will read about it in details in a separate article.


Rules for naming a variable in C/C++ :

  • A variable name can contain any digit, alphabets or underscore(_), i.e. 0 to 9, A to Z, a to z and _.
  • A variable name cannot contain any special character (not even space).
  • A variable name cannot start with a digit, but the digits can be used anywhere else in the name.
  • A variable name must not match any of the keywords defined to the C/C++ compiler.
  • A variable name can start with underscore (_).
  • As C/C++ is case dependent language, capital and small lettered variables (example : A and a) are considered two separate variables.

Some Invalid Variable names :

1name   (starting with a digit)            

my name   (includes special character (space))     

dollar$    (includes special character ($))        

break     (predefined keyword)          

 Declaration of a variable :

Unlike python, C/C++ compiler needs details about what kind of values can be stored in a variable. This information is conveyed through the data type of the variable. Type is important as the compiler decides accordingly what operations are feasible for that variable. C++ has the following data types :

char         : stores a single character in single quotes (e.g.: ‘A’, ‘2’, ‘&’)

int             : stores an integer value (e.g.: 45, 98, -65)

float         : stores a real value (e.g.: 2.05, -6.52, 5.0)

double    : stores real value with higher precision (e.g.: 2.05, -6.52, 5.0)

boolean : stores either false (as zero (0)) or true (as any number other than 0)

void         : Its an empty type. Used to represent an empty variable.

General Syntax for declaration :

data_type variable_name;

e.g. : int a; char ch; float f; double d;

A variable can also be declared as a constant using the keyword const. A constant variable is not a lvalue and so its value cannot be changed once it is initialized. Also, a constant variable must be initialized as soon as it is declared. See the following code for explanation :

const int n ;           // error: uninitialized const variable

const int n = 10;   // ok.


Initialization of variables :


Variables can be initialized in 2 ways :

Statically at time of creation : Example :

                             int i = 10;

Dynamically during run time : Example :

                             int i = sum/count ;

This statement initializes i using the information available at run time, i.e. using the values of variables sum & count.             

 

Variables based on their scope of action :

A variable remains in memory as long as the block in which it is declared is under execution. A variable declared within a function cannot be accessed outside the function block. Depending on the scope variables can be grouped into 2 groups : Global & Local.

Global variables :

A variable declared outside all the blocks (i.e. outside all {}) is called a global variable. As it is not enclosed within any block, it is available to all the functions of the program, i.e. its scope is for the entire program.

Local variables :

A variable declared inside some block (i.e. within {}) becomes available to only that block and as soon as control comes out of the block, the variable is removed from the memory. Thus, its scope is limited to that block only.

To know more about Global & Local variables and examples, read another article on this website.

 

 

 

 

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download the pdf form of this article here.

 

 

 

 

  

 


Post a Comment