Variables in C++

A variable is simply a storage unit for our data. Just like in real life we have different containers to store different objects similarly, we require different amounts of memory to store different types of data.

Variable declaration

Rules for defining variables

  1. A variable can have alphabets, numerics, and underscore.
  2. A variable name must not start with a numeric.
  3. A variable name cannot contain white space.
  4. Any reserved keyword cannot be used as a variable name (eg. switch, for)

Types of variables in C++

  • Local variable: A local variable is declared inside a function or block. The scope of the variable is limited to the function or block.
  • Global variable: A global variable is declared outside of any block or function. It is declared at the beginning of the program and can be accessed throughout the program.
  • Static variable: A static variable is a variable that has been allocated statically which means its lifetime is the entire run of the program. In simpler words, a static variable retains its value during multiple function calls.

To learn more about different data types in c++:

I am a CS graduate and an ML enthusiast. My purpose here is to guide beginners in their programming and Machine learning journey.