Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Constants in C

Last Updated on December 29, 2023 by Ankit Kochar

Constants in the C programming language represent values that do not change during the execution of a program. They play a crucial role in enhancing code readability, maintainability, and program robustness. In C, constants can be categorized into various types, including numeric constants, character constants, and symbolic constants. Understanding the use of constants provides programmers with a powerful mechanism to create reliable and easily adaptable code.

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.
  • 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.
  • 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 are an integral part of C programming, offering a way to represent unchanging values that remain constant throughout the execution of a program. Their use contributes to code clarity, making it easier to understand, modify, and maintain. By incorporating constants, programmers can create more robust and error-resistant code, improving the overall quality and reliability of their C programs.

Frequently Asked Questions (FAQs) related to Constants in C

Some Frequently Asked Questions on Constants in C are given below.

1. Can constants be changed during program execution?
No, constants cannot be changed during program execution.

2. How are numeric constants represented in C?
Numeric constants in C can be represented as integers, floating-point numbers, or in exponential notation. For example, 42, 3.14, and 2.5e3 are numeric constants.

3. What is a character constant in C?
A character constant in C is a single character enclosed in single quotes. For example, ‘A’ or ‘7’ are character constants.

4. What is a symbolic constant, and how is it defined in C?
A symbolic constant in C is a name given to represent a constant value. It is defined using the #define preprocessor directive. For example, #define PI 3.14 defines a symbolic constant named PI with the value 3.14.

5. Why use constants in C?
Constants in C enhance code readability and maintainability by providing meaningful names for fixed values. They also contribute to program robustness by preventing accidental modifications to values that should remain constant. Constants are particularly useful in making code more understandable and adaptable to changes.

Leave a Reply

Your email address will not be published. Required fields are marked *