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
6
  • 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). Commented Sep 21, 2013 at 3: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? Commented Sep 21, 2013 at 3:46
  • #include "header.h" directives will include the contents of header.h during compilation, so they are actually part of main.c contents ? Commented Sep 21, 2013 at 8:31
  • 3ks. stackoverflow.com/questions/1433204/… gives me a lot of helps. Commented Sep 21, 2013 at 9: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. Commented Sep 21, 2013 at 13:08

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.