Tutorials

# Constants in C

A constant as the name suggest is something which does not change during its life time.

In C, a constant is basically an entity whose value once defined will never change during the program execution.

A C constant will hold its value firmly through every stage of program life cycle and would not let it change at any cost. C constants are also referred as literals.

A C constant can be classified as two types:

1. Number constant
2. Character constant

We can declare various types of constant in C as shown below:

Number Constant

• integer constant,
• decimal constant,
• floating-point constant

Integer:-

• An integer constant is made up of sequence  of digit
• Digits can be any digits from 0-9 preceded by “+ or –“ sing
• The integer constant can be represented as octal no. are hexadecimal number
• An octal integer consist of any combination of digits from 0-7 with a leading 0
• A sequence of digits preceded by OX or ox is considered as hexadecimal integer
• A hexadecimal integer is combination of digits from 0-9 and alphabets A to F or a-f

Real:-

• Real constant are values which have fractional part
• Integer number are not to sufficient to represent to values which change continues like distance , price , height , temperature  etc.
• In such case real constant are use
• Real constant are also called floating point constant

Character Constant

• character constant,
• string literals

Character constant:-

• Character constant are divided into two types
• Single character constant:-
• A single character constant contains only one character inclosed within pair of single quotation
• Each character constant has an integer values associated with it called ASCII values (American standard  code for information interchange)
• Where A=65 and a=97

String character constant:-

• The string character constant is a sequence of characters (more than one character) inclosed with double quotation mark
• The character may be alphabets, digits, symbol, and blank space.

Backslash character constant:-

• We may also use special backslash character constants.
• These constant are combination of backslash and a character enclosed within single quotes as shown below:

\n →  new line

\t  →   horizontal tab

\a  →   audible

\v  →    vertical tab

\    →    single slash

\\   →    double slash

|    →    double quotes

\r   →    carriage return

\f    →   form feed

### Define Constant in C

C allows defining constant by two ways:

1. Through #define preprocessor
2. Through const keyword