Yes, that is the general idea. Though you should note that global variables are generally considered bad practice. Read this link: Global Variables Are Bad.
Originally Posted by Jony
Ignoring the extern specifier and including an initializer means you are defining a global variable. There's a difference. Definition is where the variable is created and space is set aside for it. This should exist in exactly one .c file, and never in a header (.h) file. Declaration is simply stating the type of the variable, e.g. it's an int or float, char array, pointer to struct foo, etc. Declarations simply allow the compiler to check and make sure you're using that variable correctly.
The most confusing part is "You can ignore the extern specifier, but include an
initializer, when you declare a global variable."
No. If you put "int i = 10;" in all of your .c files, and never use extern, you end up with a separate variable for each .c file. Changing i in one file won't change it in any of the others. Your global variables will get out of sync.
so if i initialize a global variable like such
int i = 10;
does this mean i no longer need extern to make it visible on other source files?
No. It means you define the variable once, in exactly one .c file, and declare it extern in every other .c file that needs to use that global variable. Normally, this is made easier by creating a header file that declares the extern symbols for you. Whether you initialize a global variable depends on whether you want it to have a specific value when the program starts, or just be zero (uninitialized globals are always set to zero).
"You should use the extern specifier (without an initializer) when you allude to a
global variable defined elsewhere."
Does this mean that i should not initialize the global variables but
rather make them extern and initialize them in the individual source files?
Don't apologize. We were all there at one point, and you've done nothing wrong (other than use what sounds like a crappy book).
Sorry if the question seems dumb... but, i'm kind of a noob so :P
Here's a small example of how to use extern:
#ifndef foo_h__ // I'm an include guard! Use me in every header file. Read about me here: http://en.wikipedia.org/wiki/Include_guard
extern int foo;
#endif // foo_h__
int foo; // here is where I define the variable
printf("foo = %d\n", foo);