Skip to content
doubtly

2 answers

Answer: a global variable is a variable declared outside all functions (usually at the top of the program). It can be used by every function in the program, and it exists for the whole time the program runs.

#include <stdio.h>

int count = 0;          /* global variable */

void increment()
{
    count++;            /* uses the global count */
}

int main()
{
    increment();
    increment();
    printf("%d", count);   /* prints 2 */
    return 0;
}

Features:

  • Scope: the whole program, from the point of declaration onward.
  • Lifetime: from the start to the end of the program.
  • Default value: 0 if not initialised (a local variable would contain garbage).

A local variable, in contrast, is declared inside a function and can be used only there. If a local variable has the same name as a global one, the local one hides the global inside that function. Global variables are convenient for sharing data but should be used sparingly, because any function can change them, which makes errors harder to trace.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.