Constants in C
Constants-
“Constants are certainly one of the most helpful and frequently used ideas in the field of programming.” Constants are variables in a programme that are unaltered while it is running. "Literals" is another name for them.
There are basically a pair of methods for creating a constant in C language:
1. Using the pre-processor command #define: this approach provides a constant value that may be used throughout the programme. The #define command can be used in the following ways:
#define const_name value
Example-
#define PI 3.1415
2. This technique employs the const phrase to create a variable with a constant value that cannot be changed during the code is running. The following is the syntax for the const keyword:
const data_type const_name = value;
Example-
const int var = 366;
Types of Constants-
1. Integer Constant in C Language-
In C, integer constants are the digits themselves. Constants with integer values range from -32,768 to 32,767. In the C language, there are three categories of integer constants:
- Base 10 decimal constant Digits in decimal form: 0 1 2 3 4 5 6 7 8 9 .
Examples are 0 and 24.
- Base 8 octal constant 0 1 2 3 4 5 6 7 are the octal digits.
For instance: 421, 027, 233, etc.
- Base-16 hexadecimal constant Digits in Hexadecimal Format: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
For instance, 0x3f, 0x5a, 0x541, etc.
Example-
int year = 2003;
int hex_num = 0x11;
int oct_num = 234;
2. Real Constant in C Language-
Real constants, commonly termed floating points constants, are numbers with fractional portions, such as 45.25. Real Constants have a range of -3.41038 to 3.41038.
There are two ways to express real constants in writing: Form in Decimal: -2.0, 0.0000234 -0.22E-5 in exponential form.
#include <stdio.h>
int main(){
double a=2E-4;
float pi = 3.1415;
printf("value of a = %lf\n",a);
printf("value of pi = %f",pi);
return 0;
}
Output-
3. Character Constant in C Language-
Character constants in C are single characters like "a" "C," "128," and "666.6" that are wrapped in the form of single quotes (' ').
Character Constants have a range of -128 to 127.
#include <stdio.h>
int main(){
char ch = 'c';
printf("value of char is %c",ch);
return 0;
}
Output-
4. String Constant in C Language-
In the language of C, a string constant is a group of characters that are encapsulated in double quotes and can contain any combination of letters, numbers, special characters, and spaces that are empty.
Examples are "I am doing great," "18237447," "#45...!", "x," "," and "" (null string constant).
#include <stdio.h>
int main(){
char greet[] ="Hello everyone";
printf("greet=%s",greet);
return 0;
}
Output-
5. Enumeration constants in C language-
Enumeration constants are utilised to symbolise a group of connected integer constants.
Example:
enum Color {WHITE, ORANGE, BLUE};
Advantages of Constants-
In C programming, constants have a number of benefits, including:
- Constants make a programme easier to understand as they assist in assigning proper names to variables that are used repeatedly throughout the programme. This makes it less complicated to read and maintain the code.
- Constants limit unintentional changes to variables that can result in mistakes or unexpected outcomes. They improve the program's dependability and predictability by making values immutable.
- When a programme uses constants, updating the definition of the constant rather than manually altering every usage of the value simplifies programme updates. This can speed up the process and lessen the possibility of adding faults to the code.
- Constants can be implemented in substitution for variables to improve the efficiency of the code by lowering the total amount of memory accesses required to get the value. This can give rise to quicker processing times and more effective memory use.
- Code is more adaptable as well as simpler to share when constants are defined once and utilised without change across various platforms and compilers.
Disadvantages of Constants-
- Constants can take extra space regardless of whether they are not being actively used by the programme. If multiple constants are defined, in such cases memory use is a major concern for the application. Hence it becomes a cause for concern.
- Constants are unchangeable by definition therefore they cannot be altered while the code is running. This may reduce the program's adaptability while making it tougher to meet evolving needs or circumstances.
- When a constant is used across lots of files or functions, it can become necessary to specify it more than once. This leads to code duplication and the possibility of contradictions if its values are not kept up to date.