Which keyword is used to prevent any changes in the variable within a ...
The correct answer is option 'C': const.
Explanation:
In C programming, the keyword 'const' is used to declare a variable as a constant. When a variable is declared as 'const', it means that its value cannot be changed throughout the program execution. The 'const' keyword is used to indicate that a variable's value is read-only and cannot be modified.
The 'const' keyword is known as a type qualifier, which modifies the type of the variable. It can be applied to any data type such as int, float, char, etc. When a variable is declared as 'const', it must be assigned a value at the time of declaration, and this value cannot be modified later in the program.
Using the 'const' keyword has several benefits:
1. Readability: It makes the code more readable by clearly indicating that a variable is meant to be constant.
2. Avoid accidental modifications: It prevents accidental modifications of the variable's value, ensuring that the intended value remains constant throughout the program.
3. Optimization: The 'const' keyword allows the compiler to perform optimizations, as it knows that the value of the variable will not change. This can lead to improved performance in some cases.
Example:
```c
#include
int main() {
const int num = 5; // declaring 'num' as a constant
printf("num = %d\n", num);
// Trying to modify the value of 'num' will result in a compile-time error
num = 10; // Error: assignment of read-only variable 'num'
return 0;
}
```
In the above example, the variable 'num' is declared as a constant using the 'const' keyword. When we try to assign a new value to 'num', it will result in a compile-time error because the variable is read-only.
Which keyword is used to prevent any changes in the variable within a ...
const is a keyword constant in C program.