Constant’s const in C
For Complete YouTube Video: Click Here
In this class, we will understand Constant’s const in C.
So far, in our previous classes, we have discussed how to create variables.
Variables are those whose values can be changed while executing the program.
The exact opposite to variable is constants.
Table of Contents
Constant’s const in C
We cannot change the value of a constant in the program.
Types of Constants
Integer Constant: 10, 20
Floating-point Constant: 3.14, 5.26
Octal Constant: 045, 077
Hexadecimal Constant: 0x9e, 0x5f
Character Constant: ‘l’, ‘m’
String Constant: “String”, “Learning Monkey”
Ways to define Constants
There are two ways to define constants
- const keyword
- #define
In this class, we will discuss the definition of constants by using the const keyword.
Example 1
The image below is an example of defining a constant by using the const keyword.
In the above example, the first line of the ‘main function’ shows the definition of the constant of a floating type.
The name of that constant is PI and assigned a value of 3.14.
With this declaration, it is fixed that we cannot change the value of the constant.
In the second line of code, we are trying to change the value of the constant.
Will that happen?
No, the compiler will generate an error.
If that line of code is not there, the printf statement will print the constant value.