Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Characteristic of variable
3.
Syntax of variable
4.
Rules for declaring a variable
5.
Types of variables based on the scope
5.1.
Local variable
5.1.1.
Code
5.2.
Global variable
5.2.1.
Code
5.3.
Static variable
5.3.1.
Code
6.
Type of variable based on data types
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Variables

Author Shivam Verma
0 upvote
gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Variables allow us to name a piece of data that we store in memory to keep using it. For example, you are making a game, you have got a player in your game, and that player character has some position on the map. The player character can move, so we need to store the player's position as some variable in our memory so that you can use the player position when required in-game. Similarly, we use a variable in our program to store some values that need to be accessed further during the program’s execution.

Also see,  Abstract Data Types in C++

Characteristic of variable

  • Variable is used as a name of the memory location where you can store the value of a variable.
  • You can change the value of a variable at any point in the program.
  • In C++ programming, the variable must be declared before using it.
  • If you are declaring a new variable, you can initialize the variable simultaneously or later.
  • You can also do a free certification of Basics of C++.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of variable

If you want to declare a variable, you must also define its type.

For example:

//Declaring a single variable
type variable_name;

//Declaring multiple variable
type variable1,variable2,variable3;

Rules for declaring a variable

  1. Variable must begin with a character without space. Example: value.
  2. Variable can also begin with an underscore('_').Example: _value.
  3. Variable can be a combination of uppercase and lowercase letters. For example, suM and Sum are can both be used as variables.
  4. The variable should not be a language(C/C++) keyword.
  5. Blank spaces can not be used in variable names. For example, we are declaring two variables, "name" and "my name" here, we can use "name" as a variable, but we can not use "my name" as a variable.
  6. Variable names are case sensitive, so be careful using variables.
  7. Variable type can be int,float,char,bool etc.


Also, see Literals in C.

Types of variables based on the scope

  • Local variables
  • Global variables
  • Static variables

Local variable

A variable that is declared inside the function or block is known as a local variable. We can access this type of variable only inside the function. If we use it outside the function, then it gives an error. 

Code

#include <iostream>
using namespace std;
//initialising a function
void fun()
{
    int val=10;
    cout<<val<<endl;
    
}
int main() {
  
    fun();
    cout<<val<<endl; // here give an error that val is not declared
    return 0;
}

 

In the above code, You cannot access 'Val' outside of the function. If you try to access it, you will get an error('Val' is not declared).

Global variable

Variables that are declared outside the function or block are called global variables. These types of variables can be accessed anywhere in the program. Global variables are usually declared on top of all functions. This is a less secure variable than local variables.

Code

#include <iostream>
using namespace std;
int k=15; // k is a global variable.
void fun()
{
    int val=10;
    cout<<k<<endl;
    cout<<val<<endl;
    
}
int main() {

    fun();
    cout<<k<<endl;
    return 0;
}


Output

15
10
15

 

In the above code, You can access the 'k' variable throughout the program

Static variable

A variable declared with a static keyword is known as a static variable. It retains its value between the multiple function calls. The static variable is created at the start of program execution and destroyed automatically when the program's execution ends. If a static variable is not initialized, then the default value of the static variable is 0.

Code

#include <iostream>
using namespace std;
void fun()
{
    int k=15;
    static int val=10; // val is a static variable
    cout<<k<<endl;
    cout<<val<<endl;
    val++;
    k++;
}
int main() {

    fun();
    fun();
    return 0;
} 


Output: 

15
10
15
11

In the above code, you can see that the static variable 'Val' is maintaining its value throughout every function call. It happens because the Static variable is created at the start of program execution and destroyed automatically when the program's execution ends.
 

You can practice by yourself with the help of online c++ compiler.

Type of variable based on data types

Some basic variables based on data types are:

  • Bool: this type of variable stores only true or false. 
  • Int: this type of variable stores integer type values such as 12 or -12 etc.
  • Char: this type of variable stores characters such as 'a' or 'A' etc.
  • Float: this type of variable stores single-precision floating-point values such as 12.4 or -12.4.
  • Double: this type of variable stores dob precision floating-point values such as 12.42 or -12.42.

Also Read - C++ Interview Questions

Frequently Asked Questions

  1. What is the use of the variable?
    Ans: Variables are used to store the input and output during the program's execution.
     
  2. What does the variable declaration mean?
    Ans: variable declaration means we declare a variable before using the variable.
     
  3. What do you mean by the scope of a variable?
    Ans: scope of the variable means the part of the program where it can be directly accessible.
     
  4. What are the static variables?
    Ans: the variable declared with a static keyword is known as a static variable.

Key Takeaways

In this blog, we have learned about variables, the rules of declaring a variable, and the type of variables.

That's all about variables. This blog is over, but the thirst for learning is not over yet. Continue your journey of acquiring knowledge with our practice platform Coding Ninjas Studio to learn, attempt, read interview experiences, and much more. Till then, have a nice day and Happy Coding!

Previous article
File Handling in Cpp
Next article
Constants/Literals
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass