// Demonstration of global variables
#include <iostream>
using std::cout;
using std::endl;
int globe; // Global variable defined here
void func(); // Declared here, defined in Global2.cpp
int main()
{
globe = 12;
cout << globe << endl; // 12
func(); // Modifies globe
cout << globe << endl; // 47
return 0;
}
/*
g++ -c Global1.cpp Global2.cpp // create object files
g++ -c Global*.cpp
g++ Global1.o Global2.o -o Global // link object files and create executable
g++ Global*.o -o Global
rm Global1.o Global2.o // clean (remove object files)
rm Global*.o
./Global // run program
Global
12
47
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
extern int globe; // Access external global variable (defined in Global1.cpp)
// (The linker resolves the reference)
void func() // Function definition (used in Global1.cpp)
{
globe = 47;
}
/*
g++ -c Global2.cpp // create object file
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-9. Compile and run Static.cpp. Remove the static keyword from the code, compile and run it again, and explain what happens.
// Using a static variable in a function
#include <iostream>
using std::cout;
using std::endl;
void func();
int main()
{
for(int x = 0; x < 10; x++)
{func();}
return 0;
}
void func()
{
static int i = 0; // initialize at first function call
cout << "i = " << ++i << endl;
}
/*
g++ Static.cpp -o Static
./Static
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Using a local variable in a function
#include <iostream>
using std::cout;
using std::endl;
void func();
int main()
{
for(int x = 0; x < 10; x++)
{func();}
return 0;
}
void func()
{
int i = 0; // initialize at every function call
cout << "i = " << ++i << endl;
}
/*
g++ Local.cpp -o Local
./Local
i = 1
i = 1
i = 1
i = 1
i = 1
i = 1
i = 1
i = 1
i = 1
i = 1
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-10. Try to compile and link FileStatic1.cpp with FileStatic2.cpp. What does the resulting error message mean?
// File scope demonstration. Compiling and linking this file with
// FileStatic2.cpp will cause a linker error
// File scope means only available in this file:
static int fs; // global static variable (or function) has file scope
extern void func(); // Trying to reference func()
int main()
{
fs = 1;
func();
return 0;
}
/*
g++ -c FileStatic1.cpp FileStatic2.cpp // create object files
g++ -c FileStatic*.cpp
g++ FileStatic*.o -o FileStatic // link object files, create executable
/usr/bin/ld: FileStatic1.o: in function `main':
FileStatic1.cpp:(.text+0x13): undefined reference to `func()'
/usr/bin/ld: FileStatic2.o: in function `func()':
FileStatic2.cpp:(.text+0xa): undefined reference to `fs'
collect2: error: ld returned 1 exit status
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
extern int fs; // Trying to reference fs
static void func() // file scope
{
fs = 100;
}
/*
g++ -c FileStatic2.cpp // create object file
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Forward function & data declarations
#include <iostream>
using std::cout;
using std::endl;
// This is not actually external, but the
// compiler must be told it exists somewhere:
extern int i; // data declaration, not definition
extern int i; // can be declared multiple times
// extern static int i; // compile error: conflicting specifiers
extern void func(); // function declaration
extern void func(); // can be declared multiple times
// extern static void func(); // compile error: conflicting specifiers
int main()
{
i = 0;
func(); // 1
return 0;
}
int i; // The data definition
// int i; // compile error: redefinition
void func() // function definition
{
i++;
cout << i << endl;
}
// void func() {} // compile error: redefinition
/*
g++ Forward.cpp -o Forward
./Forward
1
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
char c1;
// char c2 = 'c'; // link error: `c2' redefined (reinitialized) in `const2.c'
const int i1;
// const int i2 = 10; // link error: const `i2' redef (reinit) in `const2.c'
#include "const.h"
#include <stdio.h> // for printf()
#define PI 3.14
int i = 1; // initialized in `const1.c'
const char a; // uninit const OK (by default 0), initialized in `const2.c'
const char c = 'c';
const char d = 'd';
const char* cp = &c;
// int* ip = &PI; // compile error: lvalue required as unary `&' operand
const char* const ccp = &c; // const pointer
int main()
{
printf("PI = %g\n", PI); // keeps value from `const1.c'
printf("i = %d\n", i); // initialized in `const1.c'
printf("a = '%c'\n", a); // 'a', // initialized in `const2.c'
cp = &d; // OK, non-const pointer
// *cp = 'e'; // compile error: `d' is const
// ccp = &d; // compile error: const pointer
return 0;
}
/*
gcc -c const1.c const2.c // make (create) object files
gcc -c const*.c
gcc const*.o -o const // link object files, make executable
rm const*.o // clean (delete object files)
./const
PI = 3.14
i = 1
a = 'a'
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include "const.h"
#define PI 3.14159 // redefining `PI' is OK
int i; // redefining non-const `i' OK, not reinitialized
const char a = 'a'; // redefining const `a' OK, initialized in `const2.c'
// const char c = 'c'; // link error: const `c' redefined and reinitialized
const char c; // uninitialized redefinition OK
// const char* cp = &c; // link error: `cp' redefined and reinitialized
const char* cp; // `cp' redefined (not reinitialized) OK
//const char* const ccp = &c; // link error: `ccp' redefined and reinitialized
const char* const ccp; // `ccp' redefined (not reinitialized) OK
/*
gcc -c const2.c // make (create) object file
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// char c1; // link error: non-const `c1' redefined in `Const2.cpp'
// char c2 = 'c'; // link error: non-const `c2' redefined in `Const2.cpp'
const int x = 10; // OK
#include "Const.hpp"
#include <iostream>
using std::cout;
using std::endl;
#define PI 3.14
int i; // uninitialized global variable, by default 0
// const char a; // compile error: uninitialized const
const char c = 'c';
const char d = 'd';
const char* cp = &c;
// int* ip = &PI; // compile error: lvalue required as unary `&' operand
const char* const ccp = &c; // const pointer
int main()
{
cout << "PI = " << PI << endl; // keeps value from `Const1.cpp'
cout << "i = " << i << endl; // 0
cout << "c = '" << c << "'" << endl; // keeps value from `Const1.cpp'
cp = &d; // OK, non-const pointer
// *cp = 'e'; // compile error: `d' is const
// ccp = &d; // compile error: const pointer
return 0;
}
/*
g++ -c Const1.cpp Const2.cpp // make (create) object files
g++ -c Const*.cpp
g++ Const*.o -o Const // link object files, make executable
rm Const*.o // clean (delete object files)
./Const
PI = 3.14
i = 0
c = 'c'
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include "Const.hpp" // const `x' redefined OK
#define PI 3.14159 // redefining `PI' is OK
// int i; // link error: non-const `i' redefined
const char c = 'd'; // const `c' redefined OK
// const char* cp = &c; // link error: non-const pointer `cp' redefined
const char* const ccp = &c; // const pointer `ccp' redefined OK
/*
g++ -c Const2.cpp // make (create) object file
*/