Constants

Constant

  • Constant is a quantity that does’t change with the execution of the program.
  • Constant  value remains same throughout the whole program execution.
  •  Example Int a=5;  char b=”INDIA”; etc.
  • Constant may be numbers , characters or strings.
  • Example : Z=3.2*X+8-Y;
  • Here 3.2 and 8 are constants and Z, X, Y are variable.

Types of Constants:

  1. Integer constant
  2. Real or Floating Point constant
  3. Character Constant
  4. String Constant
Integer constant Real Point constant Character constant String constant
  1. Integer Constant:
  •  Integer constant consists of a sequence of digits.
  • Integer constant must have at least one digit.
  • Integer constant must not have a decimal point.
  • Integer constant may be either positive or negative.
  • No commas or blank spaces are allowed in an integer constant .
  • Integer constant used 2 byte of memory to be stored .
  • Range of integer constant is -32768 to +32767.
  • Vaild Integer constant : 0, 53, -343, 9999.

2. Floating Point Constant:

  • Floating point constant can be written in two forms, Fractional form and exponential form.
  • Floating Point constant will occupy 4 bytes of memory.
  • Floating point constant must have at least one digit.
  • There must be a decimal point in Floating Point constant.
  • No commas or blank space are allowed in floating point constant.
  • Valid floating point constant
  • +34.53, 34.33, -32.11 and 5.0 etc. are valid floating point constant.
  • Invalid floating point constant
  • 4,23   and 34-33 etc. are invalid floating point constant.

3. Character Constant:

  • A character constant is a single character enclosed in apostrophes means single quotation marks.
  • Character constant may be an alphabet, digit or special symbol.
  • Range of Character Constant is -128 to 127.
  • Example:  ‘A’ ,  ‘4’ ,  ‘-’  ,   ‘d’ and ‘=’.

4. String Constant :

  • String Constant  is a constant that consists of any number of consecutive characters.
  • A sequence of characters enclosed by double question marks .
  • Example: “Jhon” , “INDIA”,  “DEVIL” , “ ” and “3422” etc. are example of string constant.