Placement Prep

Global and Static Variables in C: Initialization Rules

Global and static variables in C are zero-initialized by default. Learn storage-class rules, BSS vs. data segment, and placement MCQ patterns for TCS NQT and AMCAT.

By FACE Prep Team 6 min read
c-programming placement-prep storage-classes static-variables global-variables aptitude

Two rules define how the C runtime handles global and static variables before your code ever runs: both types are zero-initialized, and both persist in memory for the program’s entire lifetime.

What Happens Before main() Runs

When the operating system loads a C program, the runtime sets up several memory segments before handing control to main(). Two of these matter here.

The BSS segment (Block Started by Symbol) holds global and static variables that are either uninitialized or explicitly set to zero. The OS zeroes this segment before execution begins. This is why an uninitialized int globalVar; declared outside any function reads as 0 rather than garbage. The guarantee is not a compiler convention; it is part of the C standard. Per cppreference’s documentation on C storage duration, objects with static storage duration are zero-initialized before any other initialization takes place.

The data segment holds global and static variables that are explicitly initialized to non-zero values. These values are baked into the compiled binary at build time and loaded into RAM when the program starts.

Both segments persist for the program’s entire execution. Neither is stack-based. They don’t grow and shrink with function calls. That persistence is what makes global and static variables fundamentally different from ordinary local variables.

Global Variable Initialization

A global variable is declared outside any function. It has file scope by default, meaning code anywhere in the same .c file can read or write it.

Default initialization

If you declare a global variable without assigning a value, the compiler places it in the BSS segment and the runtime zeroes it before main() runs. The type determines what zero means:

  • int, long, short0
  • float, double0.0
  • char'\0' (null character, ASCII 0)
  • pointer types → NULL
#include <stdio.h>

int globalInt;       /* 0  */
float globalFloat;   /* 0.0 */
char globalChar;     /* '\0' */
int *globalPtr;      /* NULL */

int main() {
    printf("int: %d\n", globalInt);
    printf("float: %.1f\n", globalFloat);
    printf("char ASCII: %d\n", (int)globalChar);
    printf("ptr is null: %d\n", globalPtr == NULL);
    return 0;
}

Output:

int: 0
float: 0.0
char ASCII: 0
ptr is null: 1

Explicit initialization

You can assign any constant expression at the point of declaration. That value goes into the data segment instead of BSS:

int globalCount = 10;
char status = 'A';

One constraint worth memorising: you cannot initialize a global variable with a non-constant expression. A function call, a variable-length computation, or a runtime result are all forbidden as global initializers in C. Attempting int x = rand(); at global scope is a compile error, not a runtime one. The compiler evaluates global initializers at build time. C++ relaxes this rule; C does not.

The cppreference page on C initialization lists every valid form of initializer for objects with static storage duration, including designated initializers for structs and arrays.

The extern keyword

To access a global variable defined in file1.c from file2.c, declare it with extern in file2.c:

/* file1.c */
int sharedCount = 0;   /* definition — allocates memory */

/* file2.c */
extern int sharedCount; /* declaration — no allocation */

extern tells the compiler that the variable exists somewhere in the program; the linker will resolve the address. The definition (which allocates memory) lives in exactly one file. Placing the definition in more than one file causes a linker error.

Static Variable Initialization

The static keyword has two distinct uses that placement MCQs routinely exploit.

Static local variables

A static local variable lives inside a function but retains its value between calls. It is initialized once at program start, just like a global variable, even though it is scoped locally.

#include <stdio.h>

void counter() {
    static int count = 0;  /* initialized once, at program start */
    count++;
    printf("Count: %d\n", count);
}

int main() {
    counter();
    counter();
    counter();
    return 0;
}

Output:

Count: 1
Count: 2
Count: 3

The variable count is not reset to 0 on each call. The line static int count = 0 executes exactly once. After that, count holds whatever value it had when the function last returned.

Static file-scope variables

A variable declared static at file scope (outside all functions) has internal linkage. It is visible only within the translation unit (.c file) where it is defined. Other files cannot reach it with extern, even if they try.

/* config.c */
static int internalConfig = 42;  /* invisible to all other .c files */

This is the standard C pattern for module-private state. If a module needs persistent internal data that no other module should touch, static at file scope is the right tool.

The Key Differences Placement Tests Exploit

The table below covers the six dimensions that TCS NQT, AMCAT, and Infosys InfyTQ MCQs test. The row on re-initialization is the one that catches the most candidates off-guard.

FeatureGlobal VariableStatic Local VariableStatic File-Scope Variable
ScopeEntire program (all files with extern)Only inside the declaring functionOnly inside the declaring file
LifetimeFull program executionFull program executionFull program execution
Default value0 if uninitialized0 if uninitialized0 if uninitialized
Re-initialized on each function call?N/ANo — initialized once at program startN/A
Storage locationBSS or data segmentBSS or data segmentBSS or data segment
LinkageExternal (by default)NoneInternal

One pattern that surprises candidates: static local and global variables both live in BSS or the data segment, not on the stack. Their lifetime is the entire program. Only their scope differs.

How This Topic Appears in Campus Aptitude Papers

Three question types repeat across campus aptitude papers for TCS NQT, AMCAT, Wipro NLTH, and Infosys InfyTQ.

Type 1: Output tracing with a static counter

A function with static int x = 0; x++; gets called N times. What does the output look like?

  • The trap: candidates read static int x = 0 as “reset to zero each call” and answer “1, 1, 1” instead of “1, 2, 3.”
  • The fix: remember that the = 0 part is an initializer, not an assignment. It runs once. Subsequent calls enter the function with x already holding the value from the last exit.
  • See the worked code above (the counter() function) for the canonical example.

Type 2: Global vs. local variable shadowing

int x = 10;

void f() {
    int x = 20;
    printf("%d\n", x);  /* prints 20, not 10 */
}
  • The trap: candidates assume the global x = 10 is printed.
  • The rule: a local declaration with the same name shadows the global within that function’s scope. The local x = 20 is the one in scope inside f().
  • Placement MCQs often use different variable names at the global and local levels to test whether candidates are tracking scope or just recognising name matches.

Type 3: Uninitialized global array vs. uninitialized local array

int globalArr[5];     /* all elements 0 — guaranteed */

int main() {
    int localArr[5];  /* elements are indeterminate — reading them is undefined behaviour */
    printf("%d\n", globalArr[0]);  /* safe: 0 */
    printf("%d\n", localArr[0]);   /* undefined behaviour */
    return 0;
}
  • The trap: candidates assume that both arrays behave the same when uninitialized.
  • The rule: only objects with static storage duration (global, static) get the zero-init guarantee. Local variables on the stack start with indeterminate values. Reading them before assignment is undefined behaviour in C.
  • Aptitude questions ask which array can be safely read after declaration without explicit initialization. The answer is always the global or static one.

For a broader set of C MCQ patterns covering pointers, double pointers, and memory allocation, the C coding questions covering pointer operations and memory management walks through 10 placement-style problems with full explanations.

Array operations appear in the same papers. The array operations in C used in placement tests covers insertion, deletion, and search with Big-O analysis alongside the code.

Static Variables, Persistent State, and LLM Applications

The mental model behind static int count, a value that survives across invocations without resetting, is the same model that governs conversation state in LLM-powered applications. Each turn of a multi-turn chatbot retrieves context carried forward from the previous turn, not from a zero baseline. C teaches this pattern at the hardware level.

If you want to apply the same persistence principle at the API level (building stateful LLM sessions, managing context windows, and shipping real applications), TinkerLLM is the entry point at ₹299. The projects there start from a clean state and build toward something you can show in a placement interview.

Primary sources

Frequently asked questions

Is a static local variable initialized every time the function is called?

No. A static local variable is initialized exactly once, before the first call to the function. After that, it retains whatever value it held at the end of the previous call. This is the core behavior tested in placement output-tracing MCQs.

What is the default value of an uninitialized global variable in C?

Zero. The C standard guarantees that all variables with static storage duration are zero-initialized before the program starts. int becomes 0, float becomes 0.0, char becomes the null character, and all pointer types become NULL.

What is the difference between global and static variables in C?

A global variable has external linkage by default and is accessible from other files using the extern keyword. A static variable has restricted linkage: a static local is confined to its function, and a static file-scope variable is confined to its translation unit. Both share the same lifetime — the entire program execution.

Which memory segment stores global and static variables in C?

Uninitialized or zero-initialized global and static variables go into the BSS segment. Explicitly initialized non-zero global and static variables go into the data segment. Both segments persist for the entire duration of the program, unlike the stack.

Can a static variable be declared inside a loop or if block in C?

Yes. The static keyword can be applied to any local variable, including those inside loops or conditional blocks. The variable still occupies static storage for the program's entire lifetime and is initialized only once, regardless of how many times the enclosing block executes.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF