Saturday, October 24, 2020

Constants in 'C':

Numeric Constant: Numeric constants consist of numeric digits, they may or may not have decimal point (.).

The rules for defining numeric constants-

1.    Numeric constant should have at least one digit.

2.    No comma or space is allowed within the numeric constant.

3.    Numeric constants can either be positive or negative but default sign is always positive.

There are two types of numeric constants-

Integer constant

Integer constants are whole numbers which have no decimal point ( . )

Some valid decimal integer constants are-

123

3705

23759

Some invalid decimal integer constants are-

2.5      Illegal character ( . )

3#5     Illegal character ( # )

98 5    No blank. space allowed

0925   First digit can not be zero

8,354 Comma is not allowed'

Real (floating point) Constants:

Floating point constants are numeric constants that contain decimal point.

Some valid floating point constants are-

0.5

5.3

4000.0

0.0073

5597.

39.0807


 

Character Constants:

Character constant is a single character that is enclosed within single quotes.

Some valid character constants are-

'9'        'D'       '$'       ‘ ’         '#'

Invalid character constants are-

'four'   There should be only one character within quotes

"d"       Double quotes are not allowed

"           No character between single quotes

y          Single quotes missing

 


String Constants:

A string constant has zero, one or more than one character. A string constant is enclosed within double quotes (" "). At the end of string, \0 is 'automatically placed by the compiler.

"Kumar"

"593"

"8"

"A"

Symbolic Constants:

If we want to use a constant several times then we can provide it a name. For example if we have to use the constant 3.14159265 at many places in our program, then we can give it a name PI and use this name instead of writing the constant value everywhere. These types of constants are called symbolic constants or named constants.

A symbolic constant is a name that substitutes for a sequence of characters

These constants are generally defined at the beginning of the program as

#define name value

Here 'name' is the symbolic name for the constant, and is generally written in uppercase letters. 'value' can be numeric, character or string constant.

Some examples of symbolic constants are as-

#define MAX 100

#define PI 3.14159625

#define CH 'y'

#define NAME "Sam"

In the program, these names will be replaced by the corresponding values. These symbolic constants improve the readability and modifiable of the program.

No comments:

Post a Comment