header files and interaction between .cpp files

I'm new to programming and struggling my way through Programming: principles and practice using C++. My question is regarding the "Drill" at the end of Chapter 8. I did eventually get the code to compile and everything to work out, but I was left with some lingering confusion. The confusion is in the variable foo and what is happening with it. So, as I see it, uninitialized foo in use.cpp main() foo = 7, then main() calls print_foo() in my.cpp where foo is initialized with int foo = 0; to print foo with function print_foo. Why can't I initialize int foo = 7 in main, if I was able to initialize foo in main, why initialize it in my.cpp, why doesn't my.cpp end up printing foo == 0, how is print_foo passed foo == 7 in main(), how does extern int foo; in my.h factor into all of this? I believe this is meant to be a simple exercise showing the use of header files, but it's tripping me up.

Side note, does anyone have any suggestions for a different resource than this book. It jumps around a bit, and I believe it's more designed for people learning with grad students and professors to question. I'm still having difficulty modifying the grammar in my simple calculator program to get factorial to work from two chapters back.

Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
my.h

extern int foo;
void print_foo();
void print(int);

my.cpp

#include "my.h"
#include "std_lib_facilities.h"

int foo = 0;

void print_foo()
{
	cout << foo << '\n';
}

void print(int i)
{
	cout << i << '\n';
}

use.cpp

#include "my.h"


int main()
{
	
	foo = 7;

	print_foo();

	print(99);
}

./a.out

7
99
Why can't I initialize int foo = 7 in main
Because the variable is declared to exist in the global namespace, not as a local variable.
Writing int foo = 7 inside the main function declares a local variable, entirely different than the one declared in my.h and initialized in my.cpp

If I was able to initialize foo in main, why initialize it in my.cpp?
If you don't initialize a extern variable at the point of definition, it will be zero-initialized for you by the compiler before the program starts (ignoring thread-local variables for now.)

By this, I mean:
my.hpp
 
extern int foo; // declaration 

my.cpp
1
2
# include "my.hpp"
int foo; // definition, but implicit initialization to zero 

use.cpp
1
2
3
4
5
6
7
# include "my.hpp"
int main() { 
  // foo has already been zero-initialized here
  std::cout << foo << '\n'; // prints 0
  // assignment, not initialization -- foo already has a value.
  foo = 7;
}


Why doesn't my.cpp end up printing foo == 0? How is print_foo passed foo == 7 in main(), and how does extern int foo factor in?

The point is that foo is a global variable. There is only one, which is initialized to zero before the program starts, modified in main(), and then printed when print_foo() is called. This is the purpose of extern, which says that there is exactly one variable named foo defined somewhere, only once.
Last edited on
Perfect! Thank you for the help.
Topic archived. No new replies allowed.