ch3-Forward declarations, extern and static conflicting specifiers

Chapter_3     Exercise_3-10 Constants_in_C     Exercise_3-11







Forward.cpp     TCP1, p. 163-164         download


// 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
*/









Chapter_3     Exercise_3-10 BACK_TO_TOP Constants_in_C     Exercise_3-11



Comments

Popular posts from this blog

Contents