Basics of Global Variables in C Programming - dummies

Basics of Global Variables in C Programming

By Dan Gookin

Sometimes in C programming, a variable must be like cellular phone service: available everywhere. The variable also can be used by any function at any time. This type of variable could be called a universal variable. It could be called a worldwide variable. But in C, it’s referred to as a global variable.

How to use global variables

Global variables solve specific problems by making the variable declaration universal. That way, any function anywhere in the program can access the variable. It doesn’t have to be passed or returned from a function.

Tossing Your Age Around shows how a global variable is declared and used. The global variables age and float are affected by both functions. They can be passed to those functions, but both values cannot be returned. (C functions return only one value.) Therefore, the global variable is used as a solution.


#include <stdio.h>
void half(void);
void twice(void);
int age;
float feet;
int main()
 printf("How old are you: ");
 printf("How tall are you (in feet): ");
 printf("You are %d years old and %.1f feet tall.n", age,feet);
 printf("But you're not really %d years old or %.1f feet tall.n",age,feet);
void half(void)
 float a,h;
 printf("Half your age is %.1f.n",a);
 printf("Half your height is %.1f.n",h);
void twice(void)
 printf("Twice your age is %d.n",age);
 printf("Twice your height is %.1fn",feet);

Line 6 declares the global int variable age and the float variable feet. These are global variables because they’re declared outside of any function, up there in #include, #define, and prototyping land. The variables are then used in every function. Their values can be accessed throughout the code. Even when those values are changed in the twice() function, the main() function uses the new values.

Be aware that two printf() statements in the main() function wrap their text in Tossing Your Age Around. You don’t need to wrap those statements in a text editor; simply type them out all on a single line.

Exercise 1: Type the source code for Tossing Your Age Around into your editor, creating a new program. Build and run.

Don’t be lazy about using global variables! If you can pass a value to a function, do so! It’s proper. Too many indolent programmers declare all their variables global to “solve the problem.” That’s sloppy and improper.

Good examples of global variables include information that all functions in the program should know, such as user information or whether you’re online or whether text is displayed or hidden. In that case, consider making those variables global instead.

How to create a global structure variable

A better example of using a global variable, and a situation where global variables are completely necessary, is when passing a structure to a function. In that case, you must declare the structure as global so that all functions can access variables of that structure type.

Don’t let the massive length of Passing a Structure to a Function intimidate you! Most of the “real” programs you eventually write will be far longer!


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 5
struct bot {
 int xpos;
 int ypos;
struct bot initialize(struct bot b);
int main()
 struct bot robots[SIZE];
 int x;
 robots[x] = initialize(robots[x]);
 printf("Robot %d: Coordinates: %d,%dn",
struct bot initialize(struct bot b)
 int x,y;
 x = random();
 y = random();
 b.xpos = x;
 b.ypos = y;

To pass a structure to a function, the structure must be declared globally, which happens between Lines 7 and 10. That has to happen even before the function is prototyped, which takes place at Line 12.

The initialize() function runs from Lines 30 through 41. The structure is passed to the function and returned. Note that the structure variable must be fully defined as the argument. On Line 30, the function is given the variable name b inside the function.

The return statement at Line 40 passes the structure back to the calling function. Indeed, the initialize() function is defined as a structure bot type of function. That’s the type of value it returns.

Exercise 2: Screw your courage to the sticking place, and type all those lines of source code from Passing a Structure to a Function into your editor. Build and run.

The output demonstrates how the structure array was passed (one element at a time) to a function, modified in the function, and then returned.