Saturday, 14 November 2015

CONSTANTS

Constants in C refer to the fixed values that do not change during the execution of a program
There are basically 2 types of constants in C :
  1. Primary Constants
  2. Secondary Constants

In this lesson, we will learn only about the primary constants: Numeric and Character Constants


NUMERIC CONSTANTS


There are two types of Numeric Constants 
  1. Integer Constants
  2. Real Constants

INTEGER CONSTANTS

An integer constant refers to a sequence of digits.There are 3 types of integers
  1. Decimal : Consists of a set of digits 0 to 9
  2. Octal : Consists of any combination of digits from 0 to 7 with a leading 0
  3. Hexadecimal :
They may include alphabets A/a to F/f
These alphabets represent the numbers 10 to 15 respectively
Consists of the digits preceded by 0x or 0X
Other rules for construction Integer Constants :
  1. It must not have a decimal point
  2. It could be either positive or negative
  3. If no sign precedes, it is assumed to be positive
  4. Embedded spaces/ blanks, commas and non-numeric characters are not permitted
  5. The allowable range for integer constants is -32768 to 32767 for 16 bit machines and -2 147 483 648 to 2 147 483 647 in 32 bit machines
  6. It is also possible to store large integer constants on these machines by appending qualifiers such as U/u, L/l and UL/ul to the constants

REAL CONSTANTS

A real constant is often called a Floating Point Constant.
It could be written in two forms :
  1. Fractional Form
  2. Exponential Form
Rules for constructing Real Constants for Fractional Form
  1. It must have at-least one digit (0-9)
  2. It must have a decimal point
  3. It could be either positive or negative, by default it is positive
  4. Embedded spaces, commas are not allowed with in a real constant
Rules for constructing Real Constants for Exponential Form
  1. This form is usually used if he value of the constant is either too small or too large
  2. Here the real constant is represented in 2 parts
  3. Mantissa - the part before 'e'
  4. Exponent - the part following 'e'
The general syntax : 
mantissa e exponent
  1. Mantissa part and the exponential part should be separated by letter 'e'
  2. Mantissa part could be either positive or negative, by default it is positive
  3. Exponent must have at-least one digit which must be a positive or negative integer, by default it is positive
  4. Blank is not allowed
  5. Range of real constants expressed in Exponential Form is -3.4e38 to 3.4e38

CHARACTER CONSTANTS


There are 2 kinds of Character constants :
  1. Single Character Constants :
    • contains a single character enclosed within a pair of single quote marks 
    e.g: 
    '5'  'X'  ';'  ' '
    here '5' is character and different from number 5
  2. String Constants :
    • It is a sequence of characters enclosed in double quotes
    • The characters may be letters, numbers, special characters and blank spaces 
    e.g: 
    "Hello" "1987" "&...!" "X"
    here "X" is a string , different from character 'X'

No comments:

Post a Comment