Identifiers, Variables, and Constants

Identifiers

An identifier is an unique name given to an entity, which distinctly identifies an entity in a program at the time of its execution. Identifiers are used for the naming of variable, function, class, structure or a constant, etc. Once an identifier is declared, we can use the identifier anywhere in the program to refer to the associated value.
The general rules for constructing unique identifiers are:
  1. Names can contain letters(a-z,A-Z), digits(0-9) and underscores(_)
  2. Names must begin with a letter or an underscore (_)
  3. Names are case sensitive (testScore, testscore, Testscore are all different names)
  4. Names cannot contain whitespaces or special characters like !, #, %, etc.
  5. Reserved words (like C++ keywords, such as int) cannot be used as names

Valid and Invalid Identifers

Identifer naming conventions

  1. Variables: begin with a lowercase letter and capitalize each successive word.
  2. Constants: capitalize every letter and use underscores to separate the English words
  3. Programmer written functions: will be capitalized in the same way as variable names but they will begin with a capital letter.

Variables

A variable is a named storage location in the computer's memory for holding a piece of information. The information stored in variables may change while the program is running (hence the name "variable"). Variables are symbolic names that represent locations in the computer's random-access memory (RAM). When information is stored in a variable, it is actually stored in RAM.

Syntax

data_type variable_name = value;

Variable declaration

Take a look at this: int age; This is called variable declaration. It tells the compiler the variable's name and the data type it will hold. Memory is allocated upon variable declaration.

This line indicates the variable's name is age. The word int stands for integer, so age will only be used to hold integer numbers. The data type also determines the amount of memory space to be allocated for the variable.

Variable declaration and initialization

Take a look at this: int age = 20; This is called variable declaration and initialization. It provides a inital value 20 to age, which will be saved in the allocated memory location.

Usually, variable declaration and initialization are done together.

NOTE:
  1. In C++, all the variables must be declared before use
  2. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

Constants

Constants refer to as fixed values, unlike variables whose value can be altered, constants - as the name implies does not change, they remain constant. Constant must have to be initialized at the time of creating it, and new values cannot be assigned later to it.

Defining Constants Syntax

const data_type variable_name = value;

Example:


  1. Constants can be any of the data types.
  2. Constants name should be all uppercase with words separated by underscores (_). e.g.: TAX_RATE,