Lecture 1 Notes: Introduction 1 Compiled Languages and C++
Lecture 1 Notes: Introduction 1 Compiled Languages and C++
Lecture 1 Notes: Introduction 1 Compiled Languages and C++
January 4, 2011
6.096
1.1
At its core, a computer is just a processor with some memory, capable of running tiny
instructions like store 5 in memory location 23459. Why would we express a program as
a text le in a programming language, instead of writing processor instructions?
The advantages:
1. Conciseness: programming languages allow us to express common sequences of com
mands more concisely. C++ provides some especially powerful shorthands.
2. Maintainability: modifying code is easier when it entails just a few text edits, instead
of rearranging hundreds of processor instructions. C++ is object oriented (more on
that in Lectures 7-8), which further improves maintainability.
3. Portability: dierent processors make dierent instructions available. Programs writ
ten as text can be translated into instructions for many dierent processors; one of
C++s strengths is that it can be used to write programs for nearly any processor.
C++ is a high-level language: when you write a program in it, the shorthands are suciently
expressive that you dont need to worry about the details of processor instructions. C++ does
give access to some lower-level functionality than other languages (e.g. memory addresses).
1.2
A program goes from text les (or source les) to processor instructions as follows:
Source File
Compiler
Object File
Linker
Source File
Compiler
Object File
Executable
OS
Program in Memory
Libraries
Object les are intermediate les that represent an incomplete copy of the program: each
source le only expresses a piece of the program, so when it is compiled into an object le,
the object le has some markers indicating which missing pieces it depends on. The linker
takes those object les and the compiled libraries of predened code that they rely on, lls
in all the gaps, and spits out the nal program, which can then be run by the operating
system (OS).
The compiler and linker are just regular programs. The step in the compilation process in
which the compiler reads the le 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 modications to the source code, before being fed to the
compiler. Thus, the modied diagram is:
Source File
Preprocessor
Processed Code
Compiler
Object File
Linker
Source File
1.3
Preprocessor
Processed Code
Compiler
Object File
Executable
OS
Program in Memory
Libraries
C++ is immensely popular, particularly for applications that require speed and/or access
to some low-level features. It was created in 1979 by Bjarne Stroustrup, at rst as a set
of extensions to the C programming language. C++ extends C; our rst few lectures will
basically be on the C parts of the language.
Though you can write graphical programs in C++, it is much hairier and less portable than
text-based (console) programs. We will be sticking to console programs in this course.
Everything in C++ is case sensitive: someName is not the same as SomeName.
Hello World
In the tradition of programmers everywhere, well use a Hello, world! program as an entry
point into the basic features of C++.
2.1
The code
4 int main () {
5
std :: cout << " Hello , world !\ n " ;
7
return 0;
8 }
2.2
Tokens
Tokens are the minimals chunk of program that have meaning to the compiler the smallest
meaningful symbols in the language. Our code displays all 6 kinds of tokens, though the
usual use of operators is not present here:
Token type
Description/Purpose
Examples
Keywords
Identiers
Literals
Operators
+, -, &&, %, <<
Punctuation/Separators
Punctuation dening
structure of a program
{ } ( ) , ;
Whitespace
2.3
the
Line-By-Line Explanation
1. // 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!
2. Lines beginning with # are preprocessor commands, which usually change what code
is actually being compiled. #include tells the preprocessor to dump in the contents of
another le, here the iostream le, which denes the procedures for input/output.
4. int main() {...} denes the code that should execute when the program starts up.
The curly braces represent grouping of multiple commands into a block. More about
this syntax in the next few lectures.
5.
cout << : This is the syntax for outputting some piece of text to the screen.
Well discuss how it works in Lecture 9.
Namespaces: In C++, identiers can be dened within a context sort of a
directory of names called a namespace. When we want to access an identier
dened in a namespace, we tell the compiler to look for it in that namespace using
the scope resolution operator (::). Here, were telling the compiler to look for
cout in the std namespace, in which many standard C++ identiers are dened.
A cleaner alternative is to add the following line below line 2:
using namespace std ;
This line tells the compiler that it should look in the std namespace for any
identier we havent dened. If we do this, we can omit the std:: prex when
writing cout. This is the recommended practice.
Strings: A sequence of characters such as Hello, world is known as a string. A
string that is specied 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:
Escape Sequence
Represented Character
\a
\b
Backspace
\f
\n
\r
\t
Tab
\\
Backslash
\"
\some integer x
7. 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.
4
Note that every statement ends with a semicolon (except preprocessor commands and blocks
using {}). Forgetting these semicolons is a common mistake among new C++ programmers.
So far our program doesnt do very much. Lets tweak it in various ways to demonstrate
some more interesting constructs.
3.1
3.2
Operators
We can perform arithmetic calculations with operators. Operators act on expressions to form
a new expression. For example, we could replace "Hello, world!\n" with (4 + 2) / 3,
which would cause the program to print the number 2. In this case, the + operator acts on
the expressions 4 and 2 (its operands).
Operator types:
Mathematical: +, -, *, /, and parentheses have their usual mathematical meanings,
including using - for negation. % (the modulus operator) takes the remainder of two
numbers: 6 % 5 evaluates to 1.
Logical: used for and, or, and so on. More on those in the next lecture.
Bitwise: used to manipulate the binary representations of numbers. We will not focus
on these.
3.3
Data Types
Every expression has a type a formal description of what kind of data its value is. For
instance, 0 is an integer, 3.142 is a oating-point (decimal) number, and "Hello, world!\n"
5
is a string value (a sequence of characters). Data of dierent types take a dierent amounts
of memory to store. Here are the built-in datatypes we will use most often:
Type Names
Description
Size
Range
char
1 byte
int
Larger integer.
4 bytes
signed:
-2147483648 to
2147483647
unsigned: 0 to 4294967295
bool
Boolean (true/false).
Indi
cated with the keywords true
and false.
1 byte
double
Doubly precise
point number.
8 bytes
oating
Variables
We might want to give a value a name so we can refer to it later. We do this using variables.
A variable is a named location in memory.
For example, say we wanted to use the value 4 + 2 multiple times. We might call it x and
use it as follows:
1
2
3
4
5
6
7
8
9
10
Input
Now that we know how to give names to values, we can have the user of the program input
values. This is demonstrated in line 6 below:
1
2
3
4
5
6
7
8
9
10
11
Debugging
There are two kinds of errors youll run into when writing C++ programs: compilation
errors and runtime errors. Compilation errors are problems raised by the compiler, generally
resulting from violations of the syntax rules or misuse of types. These are often caused by
typos and the like. Runtime errors are problems that you only spot when you run the
program: you did specify a legal program, but it doesnt do what you wanted it to. These
are usually more tricky to catch, since the compiler wont tell you about them.
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.