Example: Code to represent integer values
11111111 11111111 11111111 11111111 = -1 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1 |
Example: Code to represent floating point values
11111111 11111111 11111111 11111111 = -NaN (not a number) 00000000 00000000 00000000 00000000 = 0 00000000 00000000 00000000 00000001 = 1.4013e-45 |
|
Example:
(integer) 00000000 00000000 00000000 00000001 ( = 1) (integer) 00000000 00000000 00000000 00000001 ( = 1) + -------------------------------------------------------- sum is equal = 2 |
The outcome of an expression (such as addition) depends on the encoding (type) !!!
|
import java-package; // Declare stuff |
|
// file1.C #include <iostream.h> int x; int main() { x = 1234; cout << x << endl; f(); cout << x << endl; } |
// file2.C #include <iostream.h> void f() { x = 9999; // x is not defined !!! } |
|
The C++ compiler cannot complete its job because it does NOT know the location, type and size of the variable
Compile with:
// file1.C #include <iostream.h> int x; // Definition 1 int main() { x = 1234; cout << x << endl; f(); cout << x << endl; } |
// file2.C #include <iostream.h> int x; <----- Definition 2 void f() { x = 9999; } |
|
It will work when compile with the
Solaris C++ compile :
And it will fail to compile with the GNU C++ compile :
|
|
// file1.C #include <iostream.h> int x; int main() { x = 1234; cout << x << endl; f(); cout << x << endl; } |
// file2.C #include <iostream.h> void f() { x = 9999; // Compiler does not know // address, type and size of x } |
|
That is why programming languages have variable declaration
|
Example variable definition:
int myVar; float yourVar; |
Example variable DECLARATION:
extern int myVar; extern float yourVar; |
// file1.C #include <iostream.h> int x; int main() { x = 1234; cout << x << endl; f(); cout << x << endl; } |
// file2.C #include <iostream.h> void f() { extern int x; // Tells compiler that // x is an int x = 9999; // Compile knows what // code to use for 9999 } |
It will work when compile with the
Solaris C++ compile :
And it will also work to compile with the GNU C++ compile :
|
|
// Header file vars.h int x; int y; .... |
The header file is then included at the start of each program file:
// Program file 1 #include "vars.h" .... |
// Program file 2 #include "vars.h" .... |
// Program file 3 #include "vars.h" .... |
// Program file 1 int x; int y; .... |
// Program file 2 int x; int y; .... |
// Program file 3 int x; int y; .... |
The trick is best example through an example...
// Header file vars.h #ifndef EXTERN #define EXTERN extern #endif EXTERN int x; EXTERN int y; .... |
The #ifndef EXTERN C++ pre-processer directive will test if the name EXTERN has been defined
If the name EXTERN has not been defined, the #define EXTERN extern C++ pre-processer directive will be executed (and EXTERN will be defined to be extern)
Otherwise, the the #define EXTERN extern C++ pre-processer directive will be skipped
// Program file 1 #define EXTERN #include "vars.h" .... main() .... |
// Program file 2 #include "vars.h" .... |
// Program file 3 #include "vars.h" .... |
Notice that the program file containing the main() function must contain the: #define EXTERN statement
After pre-processing, we will have:
// Program file 1 int x; int y; .... main() .... |
// Program file 2 extern int x; extern int y; .... |
// Program file 3 extern int x; extern int y; .... |
You can see that the variables are defined once (in the main file) and are declared in all other files
|
Look inside the file file1.i, file2.i, and file3.i
|
This practice benefits the programmer because it can help the programmer remove many hard to find typing-errors