C++ Tutorials

C++ – Variables, Initialization and Assignment

So, let’s bring in, the concept of Al – Jabr from the school mathematics at this point. From Al- Jabr (you guessed it right, Algebra it is 😛 😀 ), the concept of variables came into action, using which a set of values or numbers are represented. Now from the programming point of view, these variables are used more or less for the same purpose, indirectly. The difference lies in the fact that the variables used here are responsible for representing a chunk or a block of memory which in turn serves as a storage location for a set of values or numbers.
Two pretty obvious questions which arise:-

  1. Where are these variables stored?
  2. How do we implement it in C++?

Obviously, these variables in the running state of program are stored in RAM, to be more specific, maybe a stack or heap or data segment which we will be discussing later on in the succeeding articles and when it is saved, a copy of it is stored in the secondary storage.
Before answering questions, I would like to bring into light the concept of data types without which the variables cannot be implemented. The fact is that the compilers are actually not smart enough as we expect. We always need to explicitly mention the type of data which it will be storing, beforehand so that it is ready with the necessary amount of memory block allocations which the variables might require. Our data or value could be anything an integer, a character, a decimal number and many more…. Thus, there is a pretty obvious need to mention the type of data which the compiler might expect, to be stored inside a variable.
In order to declare a variable, we generally used a declarative statement such as

int x; -----> declares an integer
char c; ----> declares a character

Now, as the compiler encounters these statements, a block of memory is kept apart for it. This process is known as instantiation. Although ISO C++ 11 introduces a syntactic sugar in order to deal with the explicit mention of data types, which will be a suspense till the upcoming articles are published 😛 😀

Assignment

Let us consider a declarative statement:-

int x;

Now, we want to put in a value to x at any point of program or code. The process of assigning a particular value to a variable at any point of a program or code due to the demand of the logic of the program is known as an assignment operation. Since we declared the variable x above, now we can perform assignment operation on it in the following manner.

x = 10;

Initialization

C++ also allows defining and giving an initial value to a declared variable in the same statement. This process is known as initialization. Basically this process denotes that there is variable which will be starting off its life with an initial value stored in it. A variable can be initialized only when it is defined. Following statement illustrates the use of initialization.

int x = 19;

Here the variable starts off with the value 19.

Initialization and assignment? Same thing??

Well, according to the definition stated above, these two things are different. But to the beginners, this might appear similar in technique. But there is a suspense, which will be revealed later in the upcoming articles as to how they serve to be different. Not only that, there are some type of values which will only support initialization and not an assignment. Therefore, from today, it is to better to have a mindset that these two things are significantly different. 🙂

Now coming to CBSE board’s favorite question 😛 and an interviewer who might also expect an answer to this question…

l-value and r-value

An l-value is a value that has an address……… Address?? 221-B Baker Street…. No not at all 😀
An l- value is a value that has an address in memory i.e a value that points to storage location. One such thing which we discussed earlier in this article is a “variable”. So you guessed it right, a variable is a kind of l-value. In assignment operation, the left hand side operand of the assignment operator must always be an l-value. In a similar opposite manner, rvalue is something which must result to a single valued character, integer, or any other type of data and these values do not have an address in memory. They are just mere values which may also result from expressions. Let us consider an example.

int a = 5; ------> This is initialization
int b; ----------> This is declaration
b = 6; ----------> This is assignment

Assignment in C++

Uninitialized variables

Uninitialized variables are those variables where which have not been initialized with a value. Unlike many programming languages, C++ does not initialize variables with a default value such as 0. As soon as the variable is assigned a memory location, a garbage value is dumped into that location. Let us look at a code to get an essence of the above fact

#include<iostream>
int main()
{
	int a;
	std::cout<<a;
	return 0;
}

So what output do you expect 😛 . Even I do not know what I will be getting in this case. The answer will be given by the compilers and their configurations and the answer is not at all obvious. Every time you might get different answers or maybe a same bogus answer. I use GCC 4.7.2 32 bit release with –std switch set as ISO C++ 11 and the answer I get is 4285790. In Microsoft Visual C++ we get different garbage values when we are in release configuration of build environment. Basically, we do not give a damn as to what we are getting. The point is uninitialized variables are signs of bad programming practices. Beginners, please keep a note of this because when you start writing complex programs, there will be situations where you might scratch your head regarding the results and ultimately banging your head. Finally, you will end up hardcoding to avoid those erroneous situations. Just a favorite comic to make u realize the fact I have one comic page for you. :P.

2010-04-09-programmer_grief
So, embed in your reflexes to initialize the variables resulting in a consistent state and a one which will be easier to debug 🙂 ….. So Congratulations to you again … You learned something new. Time to party. 😛

Leave a Reply