ZetCode

C variable

last modified November 14, 2026

A variable is a named storage location whose value can change while the program runs. Every variable has a type, which determines how much space it occupies and which operations are valid on it, and a name, which the program uses to refer to it. This tutorial covers declarations and initialization, the difference between local, global, static and extern variables, the lifetime of function parameters, and the const qualifier.

Declaring a variable

A declaration names the type and the variable. The optional initializer after the equals sign gives the variable its first value.

int x = 5;

Here int is the type, x is the name, = is the assignment operator and 5 is an integer literal. The semicolon ends the statement. Names must be valid C identifiers: they start with a letter or an underscore and continue with letters, digits or underscores. C is case-sensitive, so count, Count and COUNT are three different variables.

Declaring and using variables

The first example declares five variables of three types and prints their values. Three of them are declared first and assigned later; the other two are declared and initialized in one statement.

simple.c
#include <stdio.h>

int main(void) {

    int x;
    int y, z;

    x = 5;
    y = 6;
    z = 7;

    float w = 4.5;
    double h = 5.5;

    printf("%d %d %d\n", x, y, z);
    printf("%f %f\n", w, h);

    x = 55;

    printf("%d\n", x);

    return 0;
}

Several variables of the same type may share one declaration, as int y, z; does. The assignment x = 55 near the end replaces the previous value; a variable keeps its value until it is assigned again. The %f conversion prints six digits after the decimal point by default, which is why the floating-point values appear as 4.500000 and 5.500000.

$ gcc -std=c17 -Wall -Wextra -o simple simple.c
$ ./simple
5 6 7
4.500000 5.500000
55

Initialization rules

Whether a variable starts with a defined value depends on where it is declared. Objects with static storage duration, which include global variables and static locals, are zero-initialized before the program starts. Automatic variables declared inside a function are not: their value is indeterminate until the program assigns one.

init.c
#include <stdio.h>

int global_count;

int main(void) {

    static int static_count;

    int m = 6, n = 7;
    int v, w;

    v = w = 8;

    printf("m: %d, n: %d\n", m, n);
    printf("v: %d, w: %d\n", v, w);
    printf("global_count: %d\n", global_count);
    printf("static_count: %d\n", static_count);

    return 0;
}

global_count and static_count have no initializer and still print 0, because static storage is cleared before main runs. v and w are automatic, so they are assigned before the printf calls read them. A declaration such as int x, y = 5; initializes only y; reading x before assigning it is undefined behavior, and the compiler is not required to warn about it.

$ gcc -std=c17 -Wall -Wextra -o init init.c
$ ./init
m: 6, n: 7
v: 8, w: 8
global_count: 0
static_count: 0

Local variables

A variable declared inside a function is local to that function. Its name exists only in the block where it is declared, and each call of the function gets a fresh copy. The next example declares x and y in both main and max; the pairs do not clash.

local_var.c
#include <stdio.h>

int max(int, int);

int main(void) {

    int x = 4;
    int y = 5;

    printf("%d + %d = %d\n", x, y, x + y);

    int largest = max(x, y);

    printf("largest: %d\n", largest);

    return 0;
}

int max(int x, int y) {

    return x > y ? x : y;
}

The x and y inside max are parameters, and they are separate objects from the variables with the same names in main. Changing the parameter inside max would not change the caller's variable, because C passes arguments by value. The declaration int max(int, int); before main tells the compiler the function's signature, so the call can be checked.

$ gcc -std=c17 -Wall -Wextra -o local_var local_var.c
$ ./local_var
4 + 5 = 9
largest: 5

Global variables

A variable declared outside every function has file scope: it is visible to all functions that appear after its declaration in the same file. Global variables are convenient for values shared by many functions, but they make the flow of data harder to follow and should be used sparingly.

global_var.c
#include <stdio.h>

void f(void);

int x = 10;

int main(void) {

    printf("x: %d\n", x);
    x = 11;

    printf("x: %d\n", x);

    f();

    printf("x: %d\n", x);

    return 0;
}

void f(void) {

    printf("x: %d\n", x);
    x = 12;
    printf("x: %d\n", x);
}

The output shows the same object being read and written from two functions. The first two lines come from main, the next two from f, and the last line shows that the value written by f is still there after it returns. The declaration void f(void); must precede main because f is used before it is defined.

$ gcc -std=c17 -Wall -Wextra -o global_var global_var.c
$ ./global_var
x: 10
x: 11
x: 11
x: 12
x: 12

Static local variables

Adding static to a local declaration changes its lifetime but not its scope. The variable is still visible only inside the function, yet it is created once and keeps its value between calls instead of being recreated each time.

static_var.c
#include <stdio.h>

void f(void);

int main(void) {

    f();
    f();
    f();

    return 0;
}

void f(void) {

    static int n = 0;
    int c = 0;

    n++;
    c++;

    printf("n: %d, c: %d\n", n, c);
}

Each call increments both counters, but only n survives to the next call. The initializer of a static variable runs once, before the first call, not on every entry: if it ran each time, n would always be 1. The ordinary local c is recreated with the value 0 on every call, so it always prints 1.

$ gcc -std=c17 -Wall -Wextra -o static_var static_var.c
$ ./static_var
n: 1, c: 1
n: 2, c: 1
n: 3, c: 1

Function parameters

Parameters are variables that belong to a function and receive the values of the arguments at each call. C passes every argument by value, so a parameter is a fresh object initialized with a copy of the argument; assigning to it does not affect the caller.

function_params.c
#include <stdio.h>

int add(int, int);

int main(void) {

    printf("%d\n", add(4, 5));
    printf("%d\n", add(8, 9));

    return 0;
}

int add(int x, int y) {

    return x + y;
}

The parameters x and y exist only while add runs, and each call initializes them from the arguments. The prototype uses unnamed parameter types, which is enough for the compiler to check the calls; the definition gives the parameters their names.

$ gcc -std=c17 -Wall -Wextra -o function_params function_params.c
$ ./function_params
9
17

Variables in several files

A program may be split into several translation units, each compiled from its own source file. A variable defined in one file can be used in another through an extern declaration, which tells the compiler that the object exists without allocating it again. The declaration belongs in a header, and exactly one source file provides the definition.

myfile.h
#ifndef MYFILE_H
#define MYFILE_H

extern int x;
extern int y;

#endif

The header contains only declarations: extern int x; introduces the name and its type, but no storage. The include guard prevents the declarations from being processed twice when the header is included from several places.

myfile.c
#include "myfile.h"

int x = 5;
int y = 6;

This file provides the definitions. Including the header first lets the compiler check that the definitions match the declarations. Defining variables in a header instead would give every file that includes it its own copy, and the linker would report multiple definitions once the program has more than one translation unit.

extern_var.c
#include <stdio.h>
#include "myfile.h"

int main(void) {

    printf("x: %d\n", x);
    printf("y: %d\n", y);

    return 0;
}

extern_var.c uses the two variables but does not define them, so both source files must be given to the compiler in one command. The linker then resolves the references in extern_var.c to the definitions in myfile.c.

$ gcc -std=c17 -Wall -Wextra -o extern_var extern_var.c myfile.c
$ ./extern_var
x: 5
y: 6

Constant variables

The const qualifier promises that the program will not modify the variable after its initialization. The object is still a variable with a type and a scope, but any assignment to it is a compile-time error. The compiler can therefore rely on the value never changing, and a const object with static storage duration may be placed in read-only memory.

const_var.c
#include <stdio.h>

int main(void) {

    const double pi = 3.141592653589793;
    const int days = 7;

    printf("pi = %.15f\n", pi);
    printf("a week has %d days\n", days);

    return 0;
}

A const variable must be initialized where it is declared, because there is no later chance to assign it. Unlike a macro created with #define, it has a type, a scope and an address, so it can be used like any other variable, including being passed to a function that takes a pointer to constant data.

$ gcc -std=c17 -Wall -Wextra -o const_var const_var.c
$ ./const_var
pi = 3.141592653589793
a week has 7 days

Assignment to a constant

An attempt to assign a new value is rejected while compiling, before the program can run. The next program does exactly that: it initializes limit to 10 and then assigns 20.

const_assign.c
#include <stdio.h>

int main(void) {

    const int limit = 10;

    limit = 20;

    printf("%d\n", limit);

    return 0;
}

GCC names the variable and the operation, so the mistake is easy to locate. Note that const in C does not make the value a constant expression: it cannot be used as an array size at file scope or as a case label. Use an enumeration constant or a macro for those purposes.

$ gcc -std=c17 -Wall -Wextra -o const_assign const_assign.c
const_assign.c: In function ‘main’:
const_assign.c:7:11: error: assignment of read-only variable ‘limit’
    7 |     limit = 20;
      |           ^

Source

cppreference: declarations
cppreference: storage duration
cppreference: const qualifier

This tutorial covered variables in C: declarations and initialization, the zero-initialization of static storage, local and global scope, static locals, parameters, extern declarations shared through a header, and const. The types that a variable can have are described in C floating-point types, and dynamic storage for objects whose size is known only at run time is covered in C malloc and free.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C tutorials.