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 :
In this lesson, we will learn only about the primary constants: Numeric and Character Constants
There are basically 2 types of constants in C :
- Primary Constants
- Secondary Constants
NUMERIC CONSTANTS
There are two types of Numeric Constants
- Integer Constants
- Real Constants
INTEGER CONSTANTS
An integer constant refers to a sequence of digits.There are 3 types of integers
- Decimal : Consists of a set of digits 0 to 9
- Octal : Consists of any combination of digits from 0 to 7 with a leading 0
- Hexadecimal :
They may include alphabets A/a to F/fThese alphabets represent the numbers 10 to 15 respectively
Consists of the digits preceded by 0x or 0X
Other rules for construction Integer Constants :
- It must not have a decimal point
- It could be either positive or negative
- If no sign precedes, it is assumed to be positive
- Embedded spaces/ blanks, commas and non-numeric characters are not permitted
- 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
- 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 :
Rules for constructing Real Constants for Fractional Form
- Fractional Form
- Exponential Form
Rules for constructing Real Constants for Exponential Form
- It must have at-least one digit (0-9)
- It must have a decimal point
- It could be either positive or negative, by default it is positive
- Embedded spaces, commas are not allowed with in a real constant
The general syntax :
- This form is usually used if he value of the constant is either too small or too large
- Here the real constant is represented in 2 parts
- Mantissa - the part before 'e'
- Exponent - the part following 'e'
mantissa e exponent
- Mantissa part and the exponential part should be separated by letter 'e'
- Mantissa part could be either positive or negative, by default it is positive
- Exponent must have at-least one digit which must be a positive or negative integer, by default it is positive
- Blank is not allowed
- Range of real constants expressed in Exponential Form is -3.4e38 to 3.4e38
CHARACTER CONSTANTS
There are 2 kinds of Character constants :
- Single Character Constants :
- contains a single character enclosed within a pair of single quote marks
- 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:
'5' 'X' ';' ' '
here '5' is character and different from number 5
e.g:
"Hello" "1987" "&...!" "X"
here "X" is a string , different from character 'X'
No comments:
Post a Comment