What is the Constants in C Language ??? With Free Notes

What is the Constants ???

Constants, The name clear that it has fixed value that remain the same while a program is executed. They are used to represent the mathematical constants, configuration parameters and more that remains constant during the execution of the Program.

Define and Initialize Constant

Firstly we want to define constant, then we can easily use it. For Define Constant we use “const” keywords.

    // Constant Definition
    const float PI = 3.14;
    const int MAX_VALUE = 100;
  • Here we Declare and Initialize a PI by writing a “const float PI = 3.14”.
  • Also we Declare and Initialize a “const int MAX_VALUE = 100;” by writing a “MAX_VALUE”.

Using Constants

Once constants are defined, they can be used in your code wherever you would use the corresponding literal values. Like This,

    // Using Constants
    float radius = 5.0;
    float area = PI * radius * radius;

Here we declare a variable “radius” and assign the value 5.0. Then we calculate “area” using constant “PI” and “radius”.

C Language Notes

Example : Constants

Here we take an example to understand the how the code example works for Constants.

#include <stdio.h>

int main() {
    // Constant Definition
    const float PI = 3.14;
    const int MAX_VALUE = 100;
    
    // Using Constants
    float radius = 5.0;
    float area = PI * radius * radius;
    
    printf("Value of PI: %f\n", PI);
    printf("Maximum Value: %d\n", MAX_VALUE);
    printf("Area of Circle with radius %.2f: %f\n", radius, area);
    
    return 0;
}

Leave a Comment

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

Scroll to Top