Categories: C++

Variables in C++

A variable is a name that is assigned to a memory location. It is the fundamental storage unit in a program. In C++, each variable has a type that determines the size and layout of the variable’s memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

Basic types of variables in C++ :
Type Description
boolStores either value true or false.
charTypically a single octet (one byte). This is an integer type.
intThe most natural size of integer for the machine.
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.
wchar_tA wide character type.
Rules For Declaring Variable::
  • The variable’s name contains letters, digits, and underscores.
  • The variable’s name is case-sensitive (ex var and Var both are different variables).
  • The variable name contains no whitespace or special characters (for example, #, $, %, *, etc.).
  • All variable names must begin with an alphabet letter or an underscore (_).
  • We cannot use a C++ keyword as a variable name (for example, float, double, class).
Variable Declaration, Definition, and Initialization:

A variable declaration assures the compiler that there is only one variable of the given type and name, allowing the compiler to proceed with further compilation without needing complete information about the variable.

A variable definition tells the compiler where to store the variable and how much storage to allocate.

Initialization means assigning value to an already declared variable,

int i;   // declaration
i = 10;  // initialization
Scope of Variables:

A scope is a program region, and there are three places where variables can be declared in general.

  • Local variables are variables that exist within a function or a block.
  • Formal parameters are defined in the definition of function parameters.
  • Global variables are variables that exist outside all functions.
Local Variables:

Local variables are variables declared within a function or block. They can only be used by statements within that function or block of code. Local variables are unknown to functions other than their own.

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int a=11, b=1;
   
 
   
   if(a>b){
//local variable
     int c = a - b;
   }
  
   cout << c;
 
   return 0;
}
Output:
/tmp/XDf9Q9w7we.cpp: In function 'int main()':
/tmp/XDf9Q9w7we.cpp:13:12: error: 'c' was not declared in this scope
   13 |    cout << c;
      |            ^
Global Variables:

Global variables are defined outside all functions, usually at the program’s top. The global variables will retain their value for the duration of the program.

#include <iostream>
using namespace std;
  // Global variable declaration:
   int a=11, b=1,c;
   
int main () {
   if(a>b){
     c = a - b;
   }
  
   cout << c;
 
   return 0;
}
Output:
10

Note: also read about the OOPs Concepts in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago