Internal Structure of a C or C++ Program

A C or C++ source program is a collection of one or more directives, declarations, and statements contained in one or more source files.

statements Specify the action to be performed.
directives Instruct the preprocessor to act on the text of the program. Pragma directives affect compiler behavior.
declarations Establish names and define linkage characteristics such as scope, data type, and linkage.
definitions Are declarations that allocate storage for data objects or define a body for functions. An object definition allocates storage and may optionally initialize the object.

A function declaration precedes the function body. The function body is a compound statement that can contain declarations and statements that define what the function does. The function declaration declares the name, its parameters, and the data type of the value it returns.

A program must contain at least one function declaration. If the program contains only one function declaration, the function must be called main. If the program contains more than one function declaration, only one of the functions can be called main. Any additional functions called main are ignored.

By convention, main is the starting point for running a program. The main function can in turn call other functions. A program usually stops running at the first encounter of a:

A C or C++ program can contain any number of directives, declarations, and definitions. Before the program is compiled, the preprocessor filters out preprocessor directives that may change the files. Preprocessor directives are completed, macros are expanded, and a temporary source file is created containing C or C++ statements, completed directives, declarations, and definitions.

It is sometimes useful to gather variable definitions into one source file and declare references to those variables in any source files that use them. This procedure makes definitions easy to find and change. You can also organize constants and macros into separate files, and include them into source files as needed. You can use the #include directive to imbed such source files into other source files.

Directives in a source file apply to that source file and its included files only. Each directive applies only to the part of the file (and included files) following the directive.

IBM C and C++ Compilers looks for a function called main in the source code and uses it as the entry point name. If a program contains more than one function definition, only one of these functions can be named main. If the program contains only one function definition, that function must be called main.



External Structure of a C or C++ Program
Scope of Identifier Visibility
Statement Blocks


Example of a Simple C Program
Example of a C Program Comprised of Two Source Files
Specifying Path Names for Include Files


Source Program Character Set
Reserved Keywords
ASCII Character Set