Const Keyword


The 'const' keyword in C programming is used to define constants, which are values that cannot be modified once assigned.

When a variable is declared as 'const', its value remains constant throughout the program's execution, providing read-only access to the variable.

Here's an example of using the 'const' keyword to define a constant variable:


const int MAX_VALUE = 100;

    

In this example, the 'const' keyword is used to declare a constant integer variable named 'MAX_VALUE' with a value of 100. Once assigned, the value of 'MAX_VALUE' cannot be changed.

Using the 'const' keyword has several advantages, such as preventing accidental modifications of values and enabling compiler optimizations.

It also enhances code readability by clearly indicating the intent of keeping a value constant.

It's important to note that 'const' variables must be initialized at the time of declaration, as their value cannot be modified later in the program.

Additionally, 'const' can be used with other data types like 'char', 'float', and 'double' to define constants of different types.

Loading...

Search