Variables in C++

This lesson informs about where variables are stored in computers, the acceptable variables names and the min/max memory allocated to them

Introduction

Variables are used by the computer, as specified by you in your program, to record the current state of play at each step of the execution of your program so that your computer can suspend execution of your program at any point, step away, attend to some other matter, return and then continue its execution of your program without losing any information in the process.

Where are variables held?

Variables are located in the minds of programmers and in the memory of computers. Programmers use symbolic names to describe variables in their minds, for instance:

  • “the depth of the snow on the mountain”

or

  • “the amount of money in the customer’s bank account”.

Ultimately, for reasons of efficiency, locations in computer memory are referred to by numbers often represented in awkward hexadecimal notation, for example, 0xcafebabe.

Note: The compiler assists programmers by managing the relationship between the symbolic and numeric representations of the locations of variables to reduce the number of errors that programmers would surely make if there were required to refer to every variable they have in mind purely by its current location in the memories of their computers.

C++ requires the programmer to use names constructed solely from letters chosen from a through z, A through Z, and numbers are chosen from 0 through 9. C++ considers uppercase letters to be different from lowercase letters.

Important Note: Names must start with a letter.

Thus C++ allows :

aa

to be used as a variable name. Some examples of types of variable names that are permitted by C++ include:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy