ITC Lecture 2
ITC Lecture 2
Process
Correctness, simplicity, and clarity come first (Keep It Simple Software):
Correct is better than fast.
Simple is better than complex.
Clear is better than cute.
Safe is better than insecure.
The Program Creation Process
Computers execute binary instructions. These binary instructions are known as machine
instructions or machine code. It is difficult to program in machine code.
The program creation process consists of the following steps:
Step 1 – Write the program in a computer language humans can read and understand
(like C++),
Step 2 – Save the programs in text files. Programs can be a few lines long and reside in
one file or can consist of many millions of lines of code and span thousands of files,
Step 3 – Run the source code files through a program called a compiler to generate
object code for the target computer,
Step 4 – Run the object files through a program called a linker to produce an executable
image.
The Program Creation Process
Two interesting points to note in figure above.
First, C++ adds a preprocessing step to the program creation process. The C++ preprocessor
acts upon special instructions that can be contained in the C++ source code. These special
preprocessor instructions are called preprocessor directives. These are #include, #ifndef,
#define, and #endif.
The second item of interest is the assembly code file and assembler step shown at the bottom.
Program routines can be created in other languages and compiled into object modules and
then later linked with object modules created with C++. This is often referred to as mixed-
language programming.
Translating the Code for the Computer
While you now understand “My First C++ Program” code, the computer won’t. Computers
don’t understand C++ or any other programming language. They understand only machine
language.
Three programs are used to translate your source code into an executable file that the
computer can run. These programs are, in their order of appearance:
Preprocessor
Compiler
Linker
Preprocessor
The preprocessor is a program that scans the source code for preprocessor directives such as
include directives. The preprocessor inserts into the source code all files included by the include
directives.
In this example, the iostream standard library file is included by an include directive. Therefore,
the preprocessor directive inserts the contents of that standard library file, including its
definition of the cout object, into the source code file.
Compiler
The compiler is another program that translates the preprocessed source code (the source code
after the insertions made by the preprocessor) into corresponding machine language
instructions, which are stored in a separate file, called an object file, having an .obj extension.
There are different compilers for different programming languages, but the purpose of the
compiler is essentially the same, the translation of a programming language into machine
language, no matter which programming language is involved.
The compiler can understand your code and translate it into machine language only if your
code is in the proper syntax for that programming language. C++, like other programming
languages, and indeed most human languages, has rules for the spelling of words and for the
grammar of statements.
If there is a syntax error, then the compiler cannot translate your code into machine language
instructions, and instead will call your attention to the syntax errors. Thus, in a sense, the
compiler acts as a spell checker and grammar checker.
Linker
While the object file has machine language instructions, the computer cannot run the object file
as a program.
The reason is that C++ also needs to use another code library, called the run-time library, for
common operations, such as the translation of keyboard input or the ability to interact with
external hardware such as the monitor to display a message.
The linker is a third program that combines the object file with the necessary parts of the run-
time library. The result is the creation of an executable file with an .exe extension
Typical C++ Development Environment
Let’s consider the steps in creating and executing a C++ application using a C++ development
environment (illustrated in Figure above). C++ systems generally consist of three parts: a
program development environment, the language and the C++ Standard Library. C++ programs
typically go through six phases: edit, preprocess, compile, link, load and execute.
Phase 1: Creating a Program
Phase 1 consists of editing a file with an editor program (normally known simply as an editor).
You type a C++ program (typically referred to as source code) using the editor, make any
necessary corrections and save the program on a secondary storage device, such as your hard
drive. C++ source code file names often end with the .cpp, .cxx, .cc or .C extensions (note that C
is in uppercase) which indicate that a file contains C++ source code. See the documentation for
your C++ environment for more information on file-name extensions.
Phases 2 and 3: Preprocessing and Compiling a C++ Program
In phase 2, the programmer gives the command to compile the program. In a C++ system, a
preprocessor program executes automatically before the compiler’s translation phase begins
(so we call preprocessing phase 2 and compiling phase 3). The C++ preprocessor obeys
commands called preprocessor directives, which indicate that certain manipulations are to be
performed on the program before compilation. These manipulations usually include other text
files to be compiled and perform various text replacements.
Phase 4: Linking
Phase 4 is called linking. C++ programs typically contain references to functions and data
defined elsewhere, such as in the standard libraries or in the private libraries of groups of
programmers working on a particular project. The object code produced by the C++ compiler
typically contains “holes” due to these missing parts. A linker links the object code with the
code for the missing functions to produce an executable image (with no missing pieces). If the
program compiles and links correctly, an executable image is produced.
Phase 5: Loading
Phase 5 is called loading. Before a program can be executed, it must first be placed in memory.
This is done by the loader, which takes the executable image from disk and transfers it to
memory. Additional components from shared libraries that support the program are also
loaded.
Phase 6: Execution
Finally, the computer, under the control of its CPU, executes the program one instruction at a
time.
Programs do not always work on the first try. Each of the preceding phases can fail because of
various errors.
For example, an executing program might attempt to divide by zero (an illegal operation for
whole-number arithmetic in C++). This would cause the C++ program to display an error
message. If this occurs, you would have to return to phase-1 for editing, make the necessary
corrections and proceed through the remaining phases again to determine that the corrections
fix the problem(s).
.
Annotation of a C++ Basic Program Structure
Let us start by examining first the Basic Structure of a C++ Program.
int main() {
return 0;
}
This program can be compiled successfully but it will not produce any output result, since there
is no code or statements present in the main() function.
Let’s make some annotations for each of the statements.
1) // C++ Basic Program Structure is a comment.
According to the ANSI definition, ‘end of line comment’ is implemented in the syntax of C++.
This comment starts with // and ends with the end-of-line marker. The standard C comment,
delimited by /* and */ can still be used in C++: As can be seen from statement #3 – /* Code
goes in here */ is a comment.
All comments are ignored by compiler.
2) int main() is the main( ) Function
In a C/C++ program, execution begins at main( ). You must always have one and only one
function called main( ).
The main() function tells the computer running the C++ program the starting point of its
execution and When main( ) terminates, the program is over and control passes back to the
operating system.
The main( ) function is not prototyped. Thus, different forms of main( ) may be used. For both C
and C++, the following versions of main( ) are valid:
int main()
int main(int argc)
As the second form shows, at least two parameters are supported by main( ). They are argc and
argv. (Some compilers will allow additional parameters.) These two variables will hold the
number of command-line arguments and a pointer to them, respectively. argc is an integer, and
its value will always be at least 1 because the program name is the first argument as far as C/C+
+ is concerned..
The curly braces {}, in C++ programming express grouping. They indicate the start and end of
the function bodies. Here they indicate the start and end of the main() function body.
The minimal C++ program is:
main() {}
3)/* Code goes in here */ is a line comment similar to the first statement.
This comment is the standard C format, delimited by /* and */ and this format can also be used
in C++:
4) return 0; int value returned by main() function.
Every C++ program must have a main() function that returns an int value to the computer’s
operating system indicating its completion or termination. A value of zero means a successful
completion and a non-zero value indicates a failure. Sometimes programs do not end normally,
but instead crash, such as if you run out of memory during the running of the program. The
operating system may need to handle this abnormal program termination (return non-zero int)
differently than normal termination. That is why the program tells the operating system that
this time it ended normally (return 0).
How to use Dev-C++
I – Open the Dev-C++ IDE
Advertisements
REPORT THIS AD
IV – Select “Console Application” & C++ Project
Click “Save”
cout << "My first C++ Program" << endl; <--- [4]standard output stream with [5]End of line
Annotation:
Second Revision
#include
int main() {
cout<< "Line1\n";
cout<< "Line2\n";
cout<< "Line3\n";
return 0;
#include
int main() {
cout<< "Line1\nLine2\nLine3\n";
return 0;
#include
int main() {
return 0;
}
Escape sequences
\n Newline
\t Horizontal tab
\r Carriage return
\a Alert
\\ Backslash
#include <iostream>
Output:
GFG!
Tabular Difference between Syntax and Semantic Error:
Basis Syntax Semantics
tough to catch.