How to enforce immutability to prevent the modification of values in C

1 Answer

0 votes
#include <stdio.h>
 
// const variable cannot be modified.
 
int main() {
    // Declare an immutable integer using 'const'
    const int x = 10;
 
    printf("x = %d\n", x);
 
    // Uncommenting the next line will cause a compiler error:
    // x = 20;  // ERROR: assignment of read-only variable
 
    return 0;
}
 
 
/*
run:
 
x = 10
 
*/

 



answered Jun 8 by avibootz
...