Professional Documents
Culture Documents
Discussion
1
Object files are intermediate files that represent an incomplete copy of the program: each
source file only expresses a piece of the program, so when it is compiled into an object file,
the object file has some markers indicating which missing pieces it depends on. The linker
takes those object files and the compiled libraries of predefined code that they rely on, fills
in all the gaps, and spits out the final program, which can then be run by the operating
tem(OS).
The compiler and linker are just regular programs. The step in the compilation process in
which the compiler reads the file is called parsing.
In C++, all these steps are performed ahead of time, before you start running a program. In
some languages, they are done during the execution process, which takes time. This is one
of the reasons C++ code runs far faster than code in many more recent languages.
C++ actually adds an extra step to the compilation process: the code is run through a
preprocessor, which applies some modifications to the source code, before being fed to the
compiler. Thus, the modified diagram is:
Program Body
Line-by-Line Explanation:
Comments:// indicates that everything following it until the end of the line is a
comment: it is ignored by the compiler. Another way to write a comment is to put it
between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A comment of this form
may span multiple lines. Comments exist to explain non-obvious things going on in the
code. Use them: document your code well!
Preprocessor directives: Lines beginning with a pound sign (#) and the keyword include. These are
known as preprocessor directives. The preprocessor directives are the instructions to the part of the
compiler known as preprocessor which includes some extra files (codes) to the basic source program.
Header Files: The files iostream.h and conio.h are known as the header files which contain the
definitions of some functions. The iostream.h header file contains the definition of standard
input/output streams like, cout and cin where as conio.h header file includes the definitions of
function getch(), getche() and others.
As the computer is just a dumb machine and cannot understand anything until you instruct it and the
keywords cout, cin are not understandable to the computer so the header files tells the compile that
cout is this thing and whenever used do this.
The words clrscr(), cout, and getch() are not known to the computer but the definitions of these codes
are written in header files and these definitions tell the computer how to deal with the words clrscr(),
cout, and getch(). Simply speaking the preprocessor directive #include is responsible for including the
contents of the header files into the source file.
Function: main() or int main() {...}is the function as the function is always along with the parentheses.
The main() function is the first executable function in any C++ program. No matter where the main()
function is located always the first precedence goes to the main() function and its contents. The
contents of main function are enclosed in curly braces. The void before the main function says that the
function main has no return type value and at the end of the function main() will not return any value.
The braces { and } also known as curly braces, enclose the block of code present in any function. {
is known as the opening brace and } is known as closing brace. Opening brace shows the starting of
the main or any function and closing brace shows the ending of the main or any function. The code of
each and every function is always enclosed in the curly braces.
The function clrscr() is used to clear the console screen. As you work repetitively with the console
screen and output your results continuously with out rubbing the previous output your console screen
would be filled with a lot of text and your console screen will not fit your new output correctly and you
also will not be able to examine your output clearly. So the clrscr() function helps to clean the console
screen.
The cout<<Hello World tells the computer to print the string constant Hello World on the console
screen. However, a computer cannot understand what cout is but the coding written in iostream.h
header file for cout makes the computer understand what does cout mean. The cout is the standard
Strings: A sequence of characters such as Hello, world is known as a string. A string that is
specified explicitly in a program is a string literal.
Escape sequences: The \n indicates a newline character. It is an example of an escape
sequence a symbol used to represent a special character in a text literal. Here are all the
C++ escape sequences which you can include in strings:
return 0 indicates that the program should tell the operating system it has completed
successfully. This syntax will be explained in the context of functions; for now, just include it
as the last line in the main block.
EXERCISE
1. IN THE FOLLOWING FIGURE SOME ESCAPE SEQUENCES ARE GIVEN. USE THESE ESCAPE SEQUENCE INTO THE
BASIC.CPP PROGRAM AND LIST THE OUTPUT.
2. FIND WHAT DO WE MEAN BY ERRORS?
3. REMOVE THE FOLLOWING FROM THE BASIC.CPP CODE AND SEE WHAT TYPES OF ERRORS ARE
GENERATED.
a. OPENING AND CLOSING BRACES
b. INT MAIN()
c. # INCLUDE <IOSTREAM.H>
LAB SUBMISSION
LAB MUST BE SUBMITTED WITHIN ONE WEEK