0

The C Programming Language book describes static variable's usage, and the topic what-does-static-mean-in-a-c-program

  1. A static variable inside a function keeps its value between invocations.
  2. A static global variable or a function is "seen" only in the file it's declared in

explains the same as the book, but the point 1 can works and the point 2 can't work.

my code:

header.h

static int VAL = 15;
int canShare = 1;

static void hello(char c) {
printf("header file -> VAL: %d\n", VAL);
printf("header file -> canShare: %d\n", canShare);

printf("hello func -> %d\n", c);
}

main.c

#include <stdio.h>
#include "header.h"

main() {

printf("main -> VAL: %d\n", VAL+1);
printf("main -> canShare: %d\n", canShare+1);

hello('A');
}

output

main -> VAL: 16
main -> canShare: 2
header file -> VAL: 15
header file -> canShare: 1
hello func -> 65
Community
  • 1
  • 1
xiaoke
  • 11
  • 4
  • You really shouldn't normally put a static function definition in a header (though an exception could be made for an inline static function). Each source file that uses the header gets its own copy of the function. You shouldn't normally put a static variable definition in a header. Each source file that uses the header gets its own copy of the variable. You use a header for information that must be shared between files. And a program that consists of one source file plus a header isn't sharing anything with other files — as someone else noted, it is a single translation unit (TU). – Jonathan Leffler Sep 21 '13 at 03:43
  • You also should avoid defining variables (by initializing them) in headers. It's OK to write `extern int canShare;` in a header, but writing `int canShare = 1;` in a header treads into implementation-defined, not-strictly-standard behaviour. For more details, see [What are `extern` variables in C?](http://stackoverflow.com/questions/1433204/) – Jonathan Leffler Sep 21 '13 at 03:46
  • `#include "header.h"` directives will include the contents of `header.h` during compilation, so they are actually part of `main.c` contents ? – xiaoke Sep 21 '13 at 08:31
  • 3ks. http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how gives me a lot of helps. – xiaoke Sep 21 '13 at 09:02
  • Yes; `#include "header.h"` copies the named header into the text of the TU (and any nested headers). That's the job of the C preprocessor; the C compiler proper simply sees the result of all the `#include` lines being honoured, and macros being expanded, and conditional inclusion of code (`#if` … `#elif` … `#else` … `#endif`) being managed. There's usually a way to see the preprocessor output. Sometimes you run `cpp` directly; with GCC, you can use `gcc -E` or `gcc -P`. – Jonathan Leffler Sep 21 '13 at 13:08
  • :) thanks. i have learned: the best way to share variables and functions with other files ,the differences between declare and definition variables, what TU(translation unit) and the way to see preprocessor output – xiaoke Sep 22 '13 at 03:23

3 Answers3

3

There is a subtle inaccuracy in the interpretation:

A static global variable or a function is "seen" only in the file it's declared in.

It's available only in the translation unit it's declared in. #include directives literally include the header file into the translation unit, so the TU includes the included header (as you might expect, grammatically).

rici
  • 234,347
  • 28
  • 237
  • 341
2

When you #include a header, the contents of the header is inserted into the .c file. So in your example, the variable VAL and the function hello are in the .c file, it doesn't violate the rule of static.

To be more precise, identifiers declared as static has internal linkage, which means they have their scope in the translation unit. When you #include a header, that's the same translation unit.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

static variables at global level are only visible in their own source file whether they got there via an include or were in the main file.

surender8388
  • 474
  • 3
  • 12