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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago