Last Updated on March 28, 2023 by Prepbytes
While coding out some real-life applications, many times you require to define a variable whose value should remain unchanged throughout the program. The C programming language provides support for these variables whose values can be changed, known as Constants in C. Let us learn more about the Constants in C and the different types of constants that we can define in C.
What is Constant in C?
Constant in C is defined as the value which cannot be modified during the execution of the program. The constants in C are used for representing the fixed values which do not change. Here are some examples which are constants.
- Speed of Light
- Refractive Index of Light
- Number of days in a week, etc.
Different Types of Constants in C
There are different types of Constants in C. Here are the major types of Constants in C:
- Integer Constants
- Floating Point Constants
- Character Constants
- String Constants
- Enumeration Constants
These are defined in detail below.
-
Integer Constants in C
Integer constants are used to represent whole numbers. The Integer constants in C are of different types i.e., decimal, octal, and hexadecimal.- Decimal integer constants are written using the digits 0 through 9.
Example: 123, 5673, etc. - Octal integer constants are written using the digits 0 through 7 and start with a leading 0.
Example: 0122, 344, etc. - Hexadecimal integer constants are written using the digits 0 through 9 and the letters A through F and start with a leading 0x or 0X.
Example: 1A3, 235, 9F, etc.
- Decimal integer constants are written using the digits 0 through 9.
-
Floating-Point Constants in C
Floating-point constants are used to represent real numbers. A floating-point constant can be written in decimal or exponential notation.- Decimal floating-point constants are written using the digits 0 through 9, a decimal point, and an optional exponent.
Example: 3.14, 6.022, 6.0, etc. - Exponential floating-point constants are written using the letter e or E to indicate the exponent.
Example: 6.022e23, etc.
- Decimal floating-point constants are written using the digits 0 through 9, a decimal point, and an optional exponent.
-
Character Constants in C
Character constants are used to represent individual characters. A character constant is written using single quotes (‘ ‘) and consists of a single character enclosed in the quotes.
Example: ‘A’, ‘5’, etc. -
String Constants in C
String constants are used to represent a sequence of characters. A string constant is written using double quotes (" ") and consists of one or more characters enclosed in the quotes.For example, the following are valid string constants in C: "Hello World", "123", etc.
-
Enumeration Constants in C
Enumeration constants are used to represent a set of named values. Enumeration constants are defined using the enum keyword and consist of a list of names enclosed in braces.For example, the following is a valid enumeration constant in C:
enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
In the above example, Sunday is assigned the value 0, Monday is assigned the value 1, and so on.
How to declare Constants in C
Constants in C are declared in mainly two ways. The two ways are:
- Using const Keyword
- Using #define Preprocessor Directive
These methods are explained below in detail.
-
Declaring Constants in C using const Keyword
Constants in C can be defined by using the const Keyword. The values of the variables which are defined using the const keyword cannot be changed during the execution of the program.Here is how we create constants in C using the const keyword.
const int MAX_VALUE = 100;
Here, we have declared a constant variable of name, MAX_VALUE using the const Keyword. We have also initiliazed this constant with the value of 100. As already explained, the value of this constant MAX_VALUE cannot be modified in the program.
-
Declaring Constants in C using #define
You can also use #define to create constants in C. #define is a Preprocessor Directive. Here’s an example:#define MAX_VALUE 100
In this example, we have defined a constant named MAX_VALUE with the value 100. The #define preprocessor directive replaces all occurrences of MAX_VALUE with the value 100 in the code.
Both methods of creating constants have their advantages and disadvantages. Using const is more type-safe and allows the compiler to perform optimizations. On the other hand, #define is more flexible and can be used to define complex expressions as constants.
Examples of How to Define and Use Constants in C
Constants in C can be used in the same way as we use the variables. Constants can be used in expressions, assignments, and function calls. Constants can also be used in conditional statements and loops. Here are the examples.
Example 1 of Constants in C: Using const Keyword
This examples will guide you on how to declare Constants in C using the const Keyword and also how to use it further.
#include <stdio.h> int main(void) { const float PI = 3.14; float radius = 5.0; // Using the constant PI float area = PI * radius * radius; printf("The area of circle is %f", area); return 0; }
Output:
The area of circle is 78.500000
Explanation:
The above program calculates the area of a circle. We have defined a constant float variable named PI. We have calculated the area by using this predefined constant variable. At last, we printed the area on the console screen.
Example 2 of Constants in C: Using #define
We can also use the #define for defining the constants in C.
Here is an example.
#include <stdio.h> #define PI 3.14 int main(void) { float radius = 5.0; // Using the constant PI float area = PI * radius * radius; printf("The area of circle is %f", area); return 0; }
Output:
The area of circle is 78.500000
Explanation:
In this code, we have defined a constant named a PI, but this time we have used the #define Pre-processor directive instead of const Keyword. When we run this program, it will replace the PI with 3.14. We have used this constant to calculate the area of a circle. In end, we have printed the area on the screen.
Advantages of Using Constants in C
Using constants in C has several advantages over using variables. Some of the advantages are:
- Readability: The constants in C make the code more readable(since their values cannot be changed) and self-explanatory.
- Maintenance: The usage of the constants in C makes the code more maintainable. When a constant is used at multiple places, changing its value at one place will change its value at all other places.
- Safety: Constants provide safety to the code. When a variable is used in multiple places in the code, it is possible to accidentally modify the value of the variable. This can cause unexpected behavior in the code. However, when a constant is used, the value cannot be modified, providing safety to the code.
- Optimization: It helps in the optimization of the code. When we used the Constants in C, it replaces the constant with the values. This helps in reducing the overhead of accessing the values.
Conclusion
Constants play a crucial role in the C programming language. They provide a way to represent fixed values that do not change during the execution of the program. Constants make the code more readable, maintainable, and safe. Constants can be used in expressions, assignments, conditional statements, loops, and function calls. By using constants in C, programmers can write more efficient and robust code.
Frequently Asked Questions (FAQs)
Some Frequently Asked Questions on Constants in C are given below.
Ques 1. Can constants be changed during program execution?
Ans. No, constants cannot be changed during program execution.
Ques 2. Can constants be initialized during declaration?
Ans. Yes, constants can be initialized during declaration.
Ques 3. How are constant macros declared in C?
Ans. Constant macros can be declared using the #define preprocessor directive with the const keyword.
Ques 4. What is the difference between constant variables and constant macros in C?
Ans. Constant variables are stored in memory, while constant macros are replaced by their value during compilation.
Ques 5. How are global constants declared in C?
Ans. Global constants can be declared using the extern keyword and defining the constant in a header file.
Ques 6. Can the elements of a constant string be modified during program execution?
Ans. No, the elements of a constant string cannot be modified during program execution.