Variables and constant

  • Variables enable the programmers to assign and manipulate data using symbolic names.
  • Strings and numeric values can be stored in the memory of the computer for subsequent recall.
  • Whenever the memory is used for this purpose the programmer must assign a unique name to each area in memory.
  • In brief, a variable is a data name that may be used to store a data value, for easy retrieval.
    For example, the legal identifier names in ‘C++’ are,
  • totsalary
  • Counter
  • Stdname

The maximum length of the variable is machine dependent. Usually 32 is the maximum number of characters permissible in a variable name.

Constants

  • Constant is one that has a fixed value throughout the program.
  • The constant can be viewed as a read only memory variable.
  • In a program, the constant identifier should not be changed by any manifestation of the program statements.
  • Constants are broadly classified into numeric constant and character constant.
  • An integer constant is a sequence of digits that may or may not be prefixed with a minus sign.

    Some examples are,

  • 14
  • -32
  • 0
  • 576321
  • Note that C++ does not support unary plus hence, +14 is not a valid integer.

    A real constant can be represented in 2 forms,

    1.Decimal form                 2.Exponent form
     Eg.12.75			Eg.1275E-2
    		               -14E-2
    A character constant is a single character enclosed within a pair of single quotes. 
        Eg.: ‘7’  , ‘M’
    
  • Note that the character constant ‘7’ is not the same as the integer 7.
  • A string constant is a group of characters enclosed double quotes. These characters may be letters, numbers, or any special characters.
  • Eg.: “welcome” , “2000”


    Keywords and Identifiers << Previous

    Next >> Data Types

    Our aim is to provide information to the knowledge seekers. 


    comments powered by Disqus