Variables

After deciding on the type, the next thing a programmer must do is decide on a variable name.

  • A variable name must start with a letter of the alphabet or an underscore.
  • After the first letter, there can be additional letters, digits and underscores.
  • There cannot be any spaces in a variable name.
  • Words that have a special meaning in C++ such as include or main cannot be used to name variables.
  • The name of the variable should indicate its purpose. Variable names like a, b and c are not very good names for variables unless you are using them for the three sides of a triangle.
  • If two words are joined together (you cannot have any spaces), a capital letter for the second word or an underscore can improve the clarity.
    Example: hoursWorked, state_tax, and dueDate are all good variable names.
  • Remember, C++ is case sensitive. If a variable is declared as hoursWorked, it must be written exactly the same way each time it is used.
  • It will be easier to remember the names if you develop a particular style and stick to it.

NEXT: Drill on names