Variables in C++ | Basic Building Blocks

Hello Readers,

Today, in this tutorial, we are going to cover variables in C++. These starting tutorials are the basic building blocks of C++. If you already know these concepts, you can skip this tutorial and move ahead with the next tutorials in this series. However, we advise you to go through this tutorial as this will brush up on your concepts. So, without wasting time, let’s dive into it.

Note: If you already know C++ & just want to practice, then just check out this link.

A variable is a name given to a memory location in the computer memory. It is the basic unit of storage in the program.  Below are some concepts that you have to keep in mind.

  • The value stored in the variable can be changed during the primary execution.
  • The variable is basically only a name to a memory location. All the operations done on the variable are actually done on that memory location, it is associated with.
  • In C++, all the variables, must be declared before use.

How to declare variables

Below is the syntax for variable declaration:

// Declaring a single variable
type variable_name;

// Declaring multiple variable
type variable_1, variable_2,variable_3;

Here, the type is the datatype. If you initially don’t understand data type, don’t worry, we’ll cover it in the next tutorial.

There are some rules for naming a variable in C++, they are:

A variable name can consist of alphabets (both lower case and upper case), numbers, and a ‘_’(underscore) symbol. Also, the variable name must not start with a number.

Representation of c++ variables in memory
  • Save
variables in C++

Below is the program for variable declaration & definition:

#include <iostream>
using namespace std;
int main(){
	int i;
	return 0;
}

The above program is declaring a variable ‘i’ of type ‘int’. By default, if not initialized the value of the variable ‘i’ will hold any random value called the garbage value.

Variable Declaration & variable definition

Variable declaration is a part in which the variable is just introduced to the program before its first use. A variable definition is assigning a memory location and a value. Mostly variable declaration & definition are done together, sometimes that value is a garbage value.

So, that’s all for variables in C++. There is much more detail into variables like static variables, instance variables, but to understand them, you need to understand the OOPS first. The above information is sufficient for concept & problem-solving.

If you have any doubt or find any error in the above article, do mention it in the comments or mail at cpptopics@techoutflow.com. If you enjoyed reading this article or most importantly understood the concepts, don’t forget to share it with your friends. The next tutorial will be on data types in C++. It will be out soon, till then stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap