Consider following source file:
#include <stdio.h>
int g_var_1;
int g_var_2;
// ...
void f1(void);
void f2(void);
// ...
int main(void)
{
f1();
f2();
return 0;
}
void f1(void)
{
// set the value of g_var_1
g_var_1 = 100;
}
void f2(void)
{
// read the value of g_var_1
printf("%d\n", g_var_1);
}
// ...
Is it possible to "apply promise" for some functions (within the same translation unit) that g_var_1 should be considered as read-only global variable for them? I have tried with something like:
void f2(void)
{
extern const int g_var_1;
// read the value of g_var_1
printf("%d\n", g_var_1);
}
but this yields into:
error: conflicting type qualifiers for ‘g_var_1’ extern const int g_var_1;
Essentially I would like to restrict possibility of unintended modification of global variable in more complex code (values for real are known compile-time), still without "hooking" them as functions' paramaters, like as:
void f2(const int g_some)
{
printf("%d\n", g_some);
}