C Tutorial

C Static

static on a local persists between calls. static on a file-scope name hides it from other files.

One keyword, two jobs

Inside a function, static on a local variable means the variable is initialized once and then keeps its value across later calls. At file scope — outside every function — static means internal linkage: other .c files cannot see that name.

Same word. Different places. Different meanings. This chapter is C, not C++: there are no static class members here. StudyGrid compiles the examples as C17.

A static local keeps its value

Inside a function, static int n = 0; is initialized the first time execution reaches that line. Later calls skip the initializer and see the old value. That is how a helper can hand out 1, then 2, then 3 without a global sitting in the open.

Example

#include <stdio.h>

int next_seat(void) {
  static int n = 0;
  n++;
  return n;
}

int main(void) {
  printf("%d\n", next_seat());
  printf("%d\n", next_seat());
  printf("%d\n", next_seat());
  return 0;
}

Output is 1, then 2, then 3. The static local lives until the program ends. Only next_seat can see n.

Compile at /c/try. gcc runs this in the browser.

An ordinary local resets

Drop static and the same function prints 1 three times. Each call creates a freshn, sets it to 0, adds 1, and returns. The persistence came from static, not from the name.

Example

#include <stdio.h>

int next_seat(void) {
  int n = 0;
  n++;
  return n;
}

int main(void) {
  printf("%d\n", next_seat());
  printf("%d\n", next_seat());
  printf("%d\n", next_seat());
  return 0;
}

File-scope static is internal linkage

A name declared static at file scope is private to that translation unit — that .cfile after preprocessing. Another file that writes extern int total; will not find it. The linker treats the name as local to the first file.

This tutorial’s editor compiles one file, so you cannot watch a second file fail to link here. The program below still shows the keyword: total is visible to both functions in this file, and would be invisible to any other file.

Example

#include <stdio.h>

static int total = 0;

void add(int n) {
  total += n;
}

int main(void) {
  add(3);
  add(4);
  printf("%d\n", total);
  return 0;
}

Output is 7. Without static, a file-scope int total would have external linkage: other files could declare it and share the same object. static turns that sharing off.

Local static versus file-scope static

static localstatic at file scope
Where you write itInside a functionOutside every function
LifetimeUntil the program endsUntil the program ends
Who can see itOnly that functionThis file only (internal linkage)
InitializedOnce, on first reachBefore main starts

Neither kind is a substitute for passing arguments. Use a static local when one function must remember a little state. Use file-scope static when several functions in the same file share data you do not want exported. Next: scope, which decides which n a line of code can see.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

How many field readings

A Hall probe might report tesla. 12 mT, 14 mT, 13 mT are three samples of a weak field (Earth’s field is ~50 µT, so this is a lab magnet). static int n inside the function counts calls. main does not own the counter.

Example

#include <stdio.h>

void log_reading(double tesla) {
  static int n = 0;
  n++;
  printf("reading %d: %.3f T\n", n, tesla);
}

int main(void) {
  log_reading(0.012);
  log_reading(0.014);
  log_reading(0.013);
  return 0;
}

Biology

Calories added across calls

120 kcal then 80 kcal is 200. static total remembers the day so far. A nutrition label’s “kcal” is a food calorie, 1000 physics calories, which is already a mess of units. The program only adds integers.

Example

#include <stdio.h>

int add_kcal(int kcal) {
  static int total = 0;
  total += kcal;
  return total;
}

int main(void) {
  printf("%d\n", add_kcal(120));
  printf("%d\n", add_kcal(80));
  return 0;
}

FAQ: C Static

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c static examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c static in this C C lesson (C Static).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.