TExcelTAD
Home › C › C Multiple Variables

C Declare Multiple Variables

When you need several variables of the same type, you can declare them in one statement by separating the names with commas. This keeps related values together and saves a few lines.

c · try it yourself
#include <stdio.h>

int main(void) {
    int x = 5, y = 6, z = 50;
    printf("%d\n", x + y + z);
    return 0;
}
61

That single line is exactly the same as writing three separate declarations:

c
int x = 5;
int y = 6;
int z = 50;

Mixing initialised and uninitialised names

Each name in the list gets its own (optional) initial value. Only the names followed by = are initialised:

c · try it yourself
#include <stdio.h>

int main(void) {
    int width = 4, height, depth = 2;   // height has no value yet
    height = 3;
    printf("Volume: %d\n", width * height * depth);
    return 0;
}
Volume: 24

Watch out: int a, b, c = 0; only sets c to 0. a and b are left uninitialised. If you want all three to start at zero, write int a = 0, b = 0, c = 0;.

Giving several variables the same value

You can also chain assignments. Assignment works right-to-left: c = 10 happens first, its result (10) is assigned to b, and then to a.

c · try it yourself
#include <stdio.h>

int main(void) {
    int a, b, c;
    a = b = c = 10;
    printf("a=%d b=%d c=%d\n", a, b, c);

    double r, g, bl;
    r = g = bl = 0.5;
    printf("rgb = (%.1f, %.1f, %.1f)\n", r, g, bl);
    return 0;
}
a=10 b=10 c=10
rgb = (0.5, 0.5, 0.5)

Different types need separate declarations

A declaration has exactly one base type. To create variables of different types, use one declaration per type:

c · try it yourself
#include <stdio.h>

int main(void) {
    int day = 14, month = 3, year = 2025;
    char sep = '/';
    double temperature = 18.5, humidity = 0.62;

    printf("Date: %02d%c%02d%c%d\n", day, sep, month, sep, year);
    printf("Weather: %.1f C, %.0f%% humidity\n", temperature, humidity * 100);
    return 0;
}
Date: 14/03/2025
Weather: 18.5 C, 62% humidity

The pointer trap

This one surprises many people. In a declaration, the * that makes a pointer belongs to the name, not the type:

c
int* p, q;    // p is a pointer to int, but q is a plain int!
int *p, *q;   // both are pointers

Pointers are covered later, but remember this rule — it is why many C programmers write the * next to the name.

Style advice

Situation Suggestion
Closely related values (x, y, z coordinates) One line is fine: int x = 0, y = 0, z = 0;
Unrelated values One declaration per line, each with a clear name
Variables that need comments Separate lines so each comment sits next to its variable

Tip: One declaration per line makes version-control diffs cleaner and avoids the uninitialised-variable and pointer traps described above.

Test yourself

After int a, b, c = 5; which variables are guaranteed to equal 5?

  1. a, b and c
  2. only c
  3. only a
  4. none

What does x = y = 7; do?

  1. Compares x and y to 7
  2. Sets y to 7, then x to 7
  3. Sets only x
  4. It is a syntax error

In int* p, q; what is the type of q?

  1. int*
  2. int
  3. char
  4. It is an error