Components of A Source File
Components of A Source File
Components of A Source File
Aims
/****************************************************/
void main ()
{
cout << “Hello World\n”;
}
Before converting source to object code the compiler will execute the
preprocessor. The preprocessor performs preliminary operations on C++
files before they are passed to the compiler. The preprocessor allows you to:
There are two fundamental data types in C++, real for numeric values with
decimal parts and integral for whole numbers and characters.
Examples of reals include, float and double, with the type determining the
number of bytes used to hold the variable and thus the precision and range of
values the variable can hold. The actual number of bytes used depends on
the computer system. Values used in complicated calculations that must be
exact to several decimal places, such as 12.4736, are stored in variables of
type float. Note, it is entirely possible to store whole numbers, such as 12.0,
in floats. A float declaration would appear as:
float fNumbmer;
Integrals include int, char, and long again with the type determining the
number of bytes used to hold the variable. The actual number of bytes used
depends on the computer system, except for variables of type char which
always hold 1 byte. Characters, for example ‘a’, are stored in variables of type
char, while integers, such as 47, are held in int variables. Declaration of
integers and characters appear as:
int iNumber;
char cCharacter;
The following code fragment illustrates the use of variables. First the source
code allocates two areas of memory to hold integer values:
int iFirstNumber;
int iSecondNumber;
Next, the fist variable is assigned the value 17. Then the contents of the first
variable are copied to the second:
iFirstNumber = 17;
iSecondNumber = iFirstNumber;
char cCharacter;
cCharacter = ‘A’;
Note that the variable is assigned the value ‘A’, letters in ‘’ are treated as
characters. It is not permissible to have more than one character between ‘’.
#include <string.h>
…
char cCharacters [10];
strcpy (cCharacters, “Rosetta”);
In the example above the function strcpy copies the contents of a string literal
(“Rosetta”) to a string variable (cCharacters). The double quotes indicate
to the compiler that a string is being referenced (remember, single quotes
indicate a single character).
All executable code within C++ programs are contained within functions. Even
the simplest programs, as outlined above, contain at least one function called
main (). The main function is the entry point to all C++ programs, but is not
supplied by the compiler - a developer must provide it.
code. Functions may call other functions (or even themselves) but may not
contain the definitions of other functions within themselves.
All functions, other than main (), must be known to the compiler before they
are used within a source file. Unfortunately this is not always possible, either
because you do not have access to the source code which defines the
function (as in the case of library functions) or because you do not wish to
place the function before it is first used within a file. To solve this problem C++
permits the use of function prototypes. A function prototype describes the
function to the compiler, but does not actually provide the implementation - it
is an indication of what is to follow. Note, when including header files for
libraries (e.g. iostream.h) the developer is providing a number of function
prototypes (they are contained by the header file, the preprocessor expanding
them into the source file).
The ; immediately after the closing bracket indicates that this is a prototype. If
there was an opening { then this would be the start of the function definition
itself and not just a prototype.
If the function prototype and definition are modified, but uses of it with the
program are not changed, the compiler will generate compilation errors until
the usage matches the definition.