I experimented with GCC and found that you can declare external const variables in header files, but keep them mutable in implementation files.
EDIT . It really doesn't work. The only reason I got my test code for compilation was because I did not include "header.h" in "header.c".
header.h:
#ifndef HEADER_H_ #define HEADER_H_ extern const int global_variable; #endif
header.c:
int global_variable = 17;
This seems like a very good function for storing global_variable only for header.h users, but saving their modifications through implementation ( header.c ).
NOTE. The following code is just an example of how this declaration method will prevent global_variable from being assigned.
#include "header.h" int main(void) { global_variable = 34; return 0; }
Because I have never seen technology in practice before. I'm starting to wonder if this is true.
Is this the correct behavior or is it a mistake that the GCC cannot warn me about?
c external global-variables header constants
wefwefa3
source share