Introduction Week#1
Introduction Week#1
A. Machine Languages:
Any computer can directly understand only its own machine language
(also called machine code), defined by its hardware architecture. Machine
languages generally consist of numbers (ultimately reduced to 1s and 0s).
Such languages are difficult for humans.
B. Assembly Languages:
Programming in machine language was simply too slow and tedious for
most programmers. Instead, they began using English-like abbreviations
to represent elementary operations. These abbreviations formed the basis
of assembly languages. Translator programs called assemblers were
developed to convert assembly- language programs to machine language.
Although assembly language code is clearer to humans, it’s
incomprehensible to computers until translated to machine language.
C. High-Level Languages:
C and C++:
C++ evolved from C, which is available for most computers and is hardware
independent. With careful design, it’s possible to write C programs that
are portable to most computers.
C11 is the latest ANSI standard for the C programming language. It was
developed to evolve the C language to keep pace with increasingly powerful
hardware and ever more demanding user requirements. C11 also makes
C more consistent with C++.
Let’s look at a very simple C++ program. This program is called FIRST, so its
source file is FIRST.CPP. It simply prints a sentence on the screen. Here it is:
Example:
#include <iostream.h>
int main()
return 0;
Despite its small size, this program demonstrates a great deal about the
construction of C++ programs. Let’s examine it in detail.
Functions:
Functions are one of the fundamental building blocks of C++. The FIRST program
consists almost entirely of a single function called main(). The only parts of this
program that are not part of the function are the first two lines—the ones that
start with #include and using. A function can be part of a class, in which case it
is called a member function. However, functions can also exist independently of
classes. Functions that are separate standalone entities, as main() is here.
Programming Fundamental Week#1
Function Name:
The parentheses following the word main are the distinguishing feature of a
function. Without the parentheses the compiler would think that main refers to
a variable or to some other program element. When we discuss functions in the
text, we’ll follow the same convention that C++ uses: We’ll put parentheses
following the function name. The parentheses aren’t always empty. They’re used
to hold function arguments: values passed from the calling program to the
function. The word int preceding the function name indicates that this particular
function has a return value of type int.
When you run a C++ program, the first statement executed will be at the
beginning of a function called main(). (At least that’s true of the console mode
programs in this book.) The program may consist of many functions, classes,
and other program elements, but on startup, control always goes to main(). If
there is no function called main() in your program, an error will be reported when
you run the program. In most C++ programs, main() calls member functions in
various objects to carry out the program’s real work. The main() function may
also contain calls to other standalone functions.
Program Statements:
Programming Fundamental Week#1
The program statement is the fundamental unit of C++ programming. There are
two statements in the FIRST program: the line
return 0;
The first statement tells the computer to display the quoted phrase. Most
statements tell the computer to do something. In this respect, statements in C++
are similar to statements in other languages. In fact, as we’ve noted, the majority
of statements in C++ are identical to statements in C.
A semicolon signals the end of the statement. This is a crucial part of the syntax
but easy to forget. In some languages (like BASIC), the end of a statement is
signaled by the end of the line, but that’s not true in C++. If you leave out the
semicolon, the compiler will often (although not always) signal an error.
The last statement in the function body is return 0;. This tells main() to return
the value 0 to whoever called it, in this case the operating system or compiler. In
older versions of C++ you could give main() the return type of void and dispense
with the return statement, but this is not considered correct in Standard C++.
Whitespace:
We mentioned that the end of a line isn’t important to a C++ compiler. Actually,
the compiler ignores whitespace almost completely. Whitespace is defined as
spaces, carriage returns, linefeeds, tabs, vertical tabs, and form feeds. These
characters are invisible to the compiler. You can put several statements on one
line, separated by any number of spaces or tabs, or you can run a statement
over two or more lines. It’s all the same to the compiler. Thus the FIRST program
could be written this way:
#include <iostream>
using
namespace std;
int main ()
return 0;
There are several exceptions to the rule that whitespace is invisible to the
compiler. The first line of the program, starting with #include, is a preprocessor
directive, which must be written on one line. Also, string constants, such as
“Every age has a language of its own”, cannot be broken into separate lines. (If
you need a long string constant, you can insert a backslash (\) at the line break
or divide the string into two separate strings, each surrounded by quotes.)
Programming Fundamental Week#1
The operator << is called the insertion or put to operator. It directs the contents
of the variable on its right to the object on its left. In FIRST it directs the string
constant “Every age has a language of its own\n” to cout, which sends it to the
display.