CONSTANTS
• Constants are also known as literals
in C.
• Constants are quantities whose
values do not change during program execution.
• There are two types of constants:
1. Numeric constants
2. Character constants
• The const keyword is
used to declare a constant, as shown below:
int
const a = 1;
const
int a =2;
• The keyword const can
be declared before or after the data type.
1. NUMERIC CONSTANTS
• Numeric constants are of two types:
i.
Integer
constants
ii.
Floating
point constants
i.
Integer Constants
• The
whole numbers which have no fraction part (decimal point) or comma is called integer
constants.
• An integer represents a value that
is counted like the number of students in a class.
• Negative integer constant must be
preceded by a negative sign but a plus sign is optional for non-negative
integers. Some examples of valid integer constants are:
• 0
• 765
• -987
• +4563
Following are invalid integer constants for the
reason indicated:
• 6.6758
• -56.8
• 18#3
• 082
• 1(blank)
9
• 34,
56
• In
above example, character (#), blank space,(.) , (,),(-+) are illegal characters
and 0 cannot be the first digits of integer constants.
ii.
Floating Point Constants
• Floating
point constants are also called real constants.
• Floating
point constants are used to represent values that are measured like the height
of a person which might have a value of 166.75 cm, as opposed to integer
constants which are used to represent values that are counted.
• A
floating point constant consists of either a fraction form or the exponent or
the both. It may have either sign (+,-).
• Some
valid floating points are as given;
0.5,
11.0, 8905.2, 66668e2, -52.34,-0.123
• The
following are some invalid floating point constant;
• 85——————–
Missing decimal point (.)
• -1/2——————-
Illegal characters (/)
• .59,
45.4—————Illegal character (.,,) and no digits to left of the decimal point.
• Some
valid floating pint constants in the exponent forms are given below.
• 8.85e5——is
equivalent to 885000.
• 6.6E2——–is
equivalent to 660.
5.3e-6——-is equivalent to -530000.
2.
CHARACTER CONSTANTS:
• Character constants are enclosed within
single quotes such as ‘a’, ‘cd’,
‘%’ represent character constants.
• It usually includes:
• Digits 0 through 9
• Uppercase letters A through Z
• Lowercase letters a through z
• Punctuation symbols such as
semicolon (;)
• Comma (,)
• Period (.)
Special
symbols such as +,-, =,> etc.
• #include <stdio.h>
• void main() {
• int const RED=5, YELLOW=6, GREEN=4, BLUE=5;
• printf("RED = %d\n", RED);
• printf("YELLOW = %d\n", YELLOW);
• printf("GREEN = %d\n", GREEN);
• printf("BLUE = %d\n", BLUE);
• getch();
• }
• This will produce following
results
• RED = 5
• YELLOW = 6
• GREEN = 4
• BLUE = 5
No comments:
Post a Comment