Basics of the Multivariable in C Programming

By Dan Gookin

Some things just belong together, like your name and address. You can craft such a relationship in C programming by using parallel arrays or specifically named variables. But that’s clunky. A better solution is to employ a structure, as demonstrated in One Variable, Many Parts.


#include <stdio.h>
int main()
 struct player
 char name[32];
 int highscore;
 struct player xbox;
 printf("Enter the player's name: ");
 printf("Enter their high score: ");
 printf("Player %s has a high score of %dn",,xbox.highscore);

Exercise 1: Without even knowing what the heck is going on, type One Variable, Many Parts into your editor to create a new program. Build and run.

Here’s how the code in One Variable, Many Parts works:

Lines 5 through 9 declare the player structure. This structure has two members — a char array (string) and int — declared just like any other variables, in Lines 7 and 8.

Line 10 declares a new variable for the player structure, xbox.

Line 13 uses sacnf() to fill the name member for the xbox structure variable with a string value.

Line 15 uses scanf() to assign a value to the highscore member in the xbox structure.

The structure’s member values are displayed at Line 17 by using a printf() function. The function is split between two lines with a backslash at the end of Line 17; variables for printf() are set on Line 18.