Answers/Solutions to Exercises in Chapter 2, Exercise 1

E1: Can a global variable be defined in a C/C++ program that is not accessible by a certain function?

A1: Yes. There are two possible situations. If a variable is defined outside any function or a class, it is global (since it is not local to any block). It may not be accessible from any function of the program that is located in a different source file (we say may not be accessible for it may --- do you know how to make it accessible?) . It will not be accessible from any function located within the same source file but located above the place of definition. For instance

pic1.jpg (30752 bytes)
variable x is global in the program separated into two source files, x1.c and x2.c. The variable is not accessible in A(), it is accessible in B(), not accessible in C() nor D().

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 2 Index