Constants are those variables that do not change anytime during the value program execution. Whenever you declare a constant, its value remains fixed during the execution of the program. If there is an attempt to change its value then there is an error in the program.
Constants in C language are of two types.
- Constant Literals
- Constant Variables
These two types of constants are being explained in detail.
Constant Literals
Constant literals are values that you use directly in the program. For example, look at the given code below.
a = b+5; |
In the above given statement 5 is a constant literal. It has been directly used in the program. It can not be changed during the execution of the program. Think of constant literals such as direct values that can not be changed. It is generally not advised to use literal constants. Say you have used a literal constant in many places in the program, now if you need to change this constant, you can find it manually and find the program There is a need to change everywhere. Therefore you should use literal constants to minimize it.
Also Read
[su_posts template=”templates/list-loop.php” posts_per_page=”5″ tax_term=”1834″ order=”desc”]
Constant Variables
Constant variables you declare yourself like variables. The advantage of using Constant variables is that if you have to change constant later, then you do not need to change it in many places in the program. You just change the value of constant variable and in that program
Using const Keyword
You can declare constant variables even by const keyword in C language. If you want to use constant variable only in a function, then this variable can declare constant variable. The use of const keyword in C language is being explained by the example below.
#include <stdio.h> int main() {const int a=7; const int b=7;int c; /* Adding two constants */ c = a+b; printf(“Result is : %d”,a); return 0;} |
The above program produces the below given output.
Result is : 14 |
Types of C Constants
There are 4 types of constants given below in the C language.
- Integer Constants
- Floating-Point Constants
- Character Constants
- String Constants
No comments:
Post a Comment