Variables and Constants

Variables & Constants

  • A variable works as a container to hold some data value.
  • The values assigned to variables can be modified.
  • The rules for forming variable names are quite simple : They must begin with a letter or underscore ( _ ) and can be followed by any combination of letters (upper- or lowercase), underscores, or the digits 0–9.

The following is a list of valid variable names.


You can assign long names to variables, but only the first few characters may be significant. It may not bre logical to use variable names that are too long.

thenumberOfItemsSoldInThisMonth = theNumberOfItemsSoldByEndOfThisMonth – theNumberOfItemsSoldByEndOfLastMonth;

this line

conveys needed information with clarity.

Named Constants(Literals):

When coding very large programs, we may accidentally modify the value of a variable, which should not change. To avoid such coding errors, we can take help of const keyword. The variables whose values are not supposed to change, should be declared as constant variables.

const int year_of_birth;

Here the birth year is not supposed to change for the same person.


These are the values that we use in programs.

Integer constants :121, 34, 4356677687, 046, 0x4f, etc.

Character constants : ‘a’, ‘5’, ‘d’, ‘R’

Floating point constants : 1.2, 3.4, 3.456E-5, etc.

String constants : “user1”, “menu”, etc.

enumeration constants : enum count {one, two, three, four, five}

You may also like...