Hey Guys, In the last tutorial we have discussed variables. And Rules to constructing variable names in C programming language. Today in this tutorial we will go to discuss constant in the c programming language. So let’s begin...
Constant in C Programming
As its name sounds constant means the value that doesn’t change during the execution of the program. Or An Entity that doesn’t change when the program is executed.
Let us discuss through an example
Example Explained: - In this above example we stored an integer 5 in a memory location and named X given to it (Figure 1). At the same time, we also stored an integer no 10 at the same memory location where we stored no 5(Figure 2). So the No 10 can replace No 5 because only one value can be stored at a time at the same memory location. No 10 overwrite the earlier value which was no 5.
Here
X = Variables
5 = Constant
10 = Constant
Types of C Constants
In the C programming language, Constant Can be divided into two major categories:-
1. Primary Constant
2. Secondary Constant
In this tutorial, we only talk about primary constant integers, real, and character constant. Let us see the rules for constructing these different types of constant.
Rules for Constructing an Integer Constant
- An Integer constant must have one digit.
- In the Integer constant, there is no decimal value assigned to it.
- It can either a positive number or a negative number.
- If you can’t assign any sign on the number it assumed as a positive integer number.
- There is no comma or blank are allowed on your integer constant
- The range of the integer constant is ( -32768 to 32767)
Example of Integers constant are:-
- 724
- -486
- 7854
- -4786 etc
Rules for Constructing Character Constant
A character constant is a single digit of the alphabet, digit, or any special character which is enclosed by inverted commas. The maximum length of a character constant can be only 1 character.
Example of Character constant are:-
- ‘A’
- ‘B’
- ‘5’
- ‘*’
Note:- Both the inverted comma should point on the left. Example ‘Z’ is a valid character constant. Whereas “Z” is not a valid Constant.
Rules for Constructing Real Constant
Real Constant is also known as Floating Point Constant. There is two way to write real constant are
1. Fraction Form and 2. Exponential Form
Rule of Constructing Real Constant in Fraction Form
- A real constant must have at least 1 digit
- It must have a decimal point.
- It could either positive or negative.
- No comma or blank was allowed on it.
Ex :-
- 325.24
- 420
- -46.46
- +654.1968
Rules of Constructing Real Constant in Exponential form
The Exponential Form of a real constant is used either your value is too small or too large. In the exponential form, the real constant is represented in two parts. The part appearing before ‘e’ is called the mantissa. Whereas the part following ‘e’ is called the exponent.
The Rules are:-
- The mantissa part and the exponential form part should be separated by a letter e.
- The mantissa part may have either a positive or negative sign.
- The exponential form must have at least one digit, which is either positive or negative integer.
- In Exponential form, the Default sign is positive.
- The Range of real constants expressed in exponential form is (-3.4e38 to 3.4e38).
Ex.:
- +8.2e-5
- 6.1e8
- -10.2e+3
- -5.4e-5