L
Lu
Hello, I am wondering how to protect a global variable in a header file from
external access. So I googled and found:
"The keyword 'static' has two different uses, depending on whether it is
applied to an external variable or function or to an automatic variable.
When applied to an external variable (global) variable the scope of that
variable to the file in which it is declared. This is useful to hide buffers
and variables that are used only by functions in a particular file."
However, when I tried to teach my self and do the following exercise, it
shows me that static global variables are still available in "main.c".
[jxlu@edusrv jxlu]$ cat static_i.c
#include <stdio.h>
#include "static_i.h"
int main(int argc,char *argv[])
{
printf("i=%d\n",i);
return 0;
}
[jxlu@edusrv jxlu]$ cat static_i.h
static int i=0;
[jxlu@edusrv jxlu]$ gcc -Wall -o static_i static_i.c
[jxlu@edusrv jxlu]$ ./static_i
i=0
[jxlu@edusrv jxlu]$
It really puzzles me! Any suggestions?
Sincerely,
Lu
external access. So I googled and found:
"The keyword 'static' has two different uses, depending on whether it is
applied to an external variable or function or to an automatic variable.
When applied to an external variable (global) variable the scope of that
variable to the file in which it is declared. This is useful to hide buffers
and variables that are used only by functions in a particular file."
However, when I tried to teach my self and do the following exercise, it
shows me that static global variables are still available in "main.c".
[jxlu@edusrv jxlu]$ cat static_i.c
#include <stdio.h>
#include "static_i.h"
int main(int argc,char *argv[])
{
printf("i=%d\n",i);
return 0;
}
[jxlu@edusrv jxlu]$ cat static_i.h
static int i=0;
[jxlu@edusrv jxlu]$ gcc -Wall -o static_i static_i.c
[jxlu@edusrv jxlu]$ ./static_i
i=0
[jxlu@edusrv jxlu]$
It really puzzles me! Any suggestions?
Sincerely,
Lu