Home Previous Next

CSC100 :: Lecture Note :: Week 02
Assignments | Code | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
{GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates}

Overview

Assignments: #assessment1 (due 2/19/2017) and [program] #helloworld (due 2/19/2017) and [program] #arithmetic (due 2/26/2017)

Handout: DevC++ IDE

Code: new C++ program template | PrimitiveTypes.cpp | Constants.cpp | ArithOps.cpp


The C and C++ Compilation Process

C and C++ are high-level, 3rd-generation languages. These types of languages must be translated into a machine language in order to be executed by a CPU. The process of translating high-level language into machine language is called the compilation process.

The compilation process consists of the following steps.

   edit source code ->  compile   ->   link    ->   execute
      (editor)         (compiler)    (linker)       (loader)

Program source code is entered into a file using a text editor. After the code has been entered, a compiler program is started that translates the source into an object code file. The object code file is linked with other object code files that come with the compiler and an executable file (or program) is created. In order to execute the program, a program called the loader copies the executable file into the memory of the computer and sends an execute command to the CPU.

It should be noted that errors can occur during each step


source      +----------+      object     +--------+      executable
file   ---> | compiler | ---> file  ---> | linker | ---> file      ---+
(x.c)       +----------+      (x.o)      +--------+      (x)          |
                                             ^                        |
                                             |                        v
                                             |                    +--------+
                                             |                    | loader |
                                     Standard C Library           +--------+
                                     (stdc.lib                        |
                                          stdio.o                     |
                                          stdlib.o                    v
                                          math.o                     CPU
                                          ...)

A source file ending with ".c" contains C source code; whereas, a file ending with ".cpp" is a C++ file (note: ".C" suffix may also indicate a C++ source file). A file ending with ".h" can be both a C and C++ header file. Sometimes the suffix ".hpp" (or ".H") is used to indicate a C++ only header file.

The compiler is a program that usually consists of many phases. The first phase of compilation is called preprocessing. The preprocessor does many things, but two features that must be learned immediately are file inclusion and macro (manifest constant) definitions. After preprocessing, the compiler executes two primary steps: lexical analysis and parsing. During lexical analysis, the source code is broken up into tokens and the tokens are passed to the parser. The parser does syntax and semantic analysis, which includes the generation of object code (i.e. machine language).

The linker "combines" all object code files into an executable file (by default, named a.out on Unix systems). Typically, the object files created by your source files are linked with object files that are packaged into libraries.

Most implementations allow each step of the compilation process to be executed as a stand-alone procedure. For example, compile a source file but do not invoke the linker; execute the preprocessor only; or, invoke the linker only.

Some older compilers translate C source code into assembly language, then execute an assembler program to translate the assembly language into machine language.

Early C++ compilers (those prior to 1992) translated C++ code into C code and then executed the C compiler.

The loader reads a program (i.e. executable file) into memory. Once this is completed, it becomes a process and the CPU executes it.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Introduction to the cout Object

In order to use the C++ Streams I/O, the iostream.h header file must be included. In addition, inclusion of iomanip.h is often needed.

   #include <iostream>
   #include <iomanip>

Upon program startup, the cout object of class ostream is instantiated.

Output to the terminal screen (or the standard output) is performed using the cout object, and the left bit-shift operator << in combinations.

   cout << EXPR;
   // in many instances, EXPR needs to be inside ()'s

Multiple values can be output by executing the following.

   cout << EXPR << EXPR << EXPR << ...;

Only one EXPR is allowed after each << The << points in the direction of the data flow to cout (the standard output) and is referred to as the insertion operator.

A cout object does not automatically place spaces between values being output, and it does not automatically add a newline at the end of its output.

Both "\n" and '\n' result in a newline being printed to the standard output. In addition, the endl I/O manipulator can be used to print a newline. Example: the following statements all print a newline followed by the value of an EXPR followed by another newline:

   cout << endl << EXPR << endl;
   cout << "\n" << EXPR << "\n";
   cout << '\n' << EXPR << endl;
   cout << endl << EXPR << "\n";
   cout << '\n' << EXPR << '\n';

C and C++ I/O can be mixed on a per-character basis, but to ensure data is sent/received in the proper order a call to sync_with_stdio() should be made.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


About Escape Sequences

Escape sequences are used for the following reasons.

C and C++
  \a  Alert   \b  Backspace   \f  Formfeed
  \n  Newline   \r  Carriage return   \t  Horizontal Tab
  \v  Vertical Tab   \"  Double quote   \'  Single quote
  \\  Backslash   \?  Question Mark   \0  Null Character
There are two types of numeric escape sequences: octal and hexadecimal.
Java Escape Sequences
Java will not compile a source file if it contains invalid escape sequences. [ Example]

  \b  Backspace   \f  Formfeed   \n  Newline
  \r  Carriage return   \t  Horizontal Tab   \"  Double quote
  \'  Single quote   \\  Backslash   \uhhhh  Unicode; 4 hexadecimal digits
  \ooo  C style; 3 octal digits

All of the Java escape sequences -- except for Unicode \u -- can only be used within string and character literals.

   String doubleQuote = "\"";
   char singleQuote = '\'';
   char bestGrade = '\u0041';    // set to 'A'
   char worstGrade = '\106';     // set to 'F'

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Variables, Identifiers and Keywords

A variable is a location in the computer's memory where a value can be stored for use by a program.

An identifier is a name supplied to a variable. An identifier in C is a sequence of letters and digits. The first character must be a letter; the underscore _ counts as a letter. Upper and lower case letters are different (i.e. C is case sensitive). Good programming practice: avoid using leading underscores in identifiers. In addition, current convention is to start variable names with a lower case letter.

A keyword is a predefined identifier that has special meaning to the compiler and it is reserved by the language.

C Keywords

C reserves the following identifiers for use as keywords, they cannot be used otherwise.

   auto, break, case, char, const, continue, default, do, double,
   else, enum, extern, float, for, goto, if, int, long, register,
   return, short, signed, sizeof, static, struct, switch, typedef,
   union, unsigned, void, volatile, while
C9X indicates that bool will become a C keyword.
C++ Keywords

The following are potential C identifiers that are keywords in C++.

   and, and_eq, asm, bitand, bitor, bool, catch, class, compl, const_cast,
   delete, dynamic_cast, explicit, false, friend, inline, mutable,
   namespace, new, not, not_eq, operator, or, or_eq, private, protected,
   public, reinterpret_cast, static_cast, template, this, throw, true, 
   try, typeid, typename, using, virtual, wchar_t, xor, xor_eq

All of the C keywords are also C++ keywords.

Choosing Identifier Names

Choosing meaningful identifier names helps programs to be "self-documenting."

Generally, 1 or 2 character names are considered cryptic. Some commonly used short names are: c for characters; i, j, k for indexes; n for counters; p or q for pointers; s for strings; and x, y, z for floating-point variables.

Avoid using names that begin with underscores because the implementation may use names like that for its own purposes internally.

Abbreviations for meaningful names, if used, should be used consistently. For example, if nbr is used for number, then always use nbr instead of number.

A name should convey information as to how the variable is used and what type of data it will store. Example: item_cnt, lines_per_pg, max_input, buffer_size, and so on.

Extremely long names can convey lots of information, but they tend to make code difficult to read and maintain. Names should not exceed 31 characters in length.

Some linkers may make as few as the first six characters as significant.

Key Points

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Primitive Data Types

The primitive data types are built-in to the language. They are also referred to as basic, atomic, fundamental, base, and so on.

The C and C++ languages support the following primitive data types.

* The bool primitive data type is in C++ only; however, it has been added to the newest versions of C.

The   char  short   int  long   primitive data types are  integral  data types; whereas,   float  double  long double   are  floating-point  types.

When you define a variable, memory is allocated.

The amount memory allocated is implementation-dependent. For example, on system A an int may take 4 bytes; whereas on system B is takes 2 bytes.

The amount of memory allocated dictates the minimum and maximum values that can be stored in variables.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Constants

Constants are values that are set at compile-time and cannot be changed at run-time (i.e. they are immutable).

Each constant value is defined to be a specific type.

By default, an integral number is type int. If it is too large (or small) of a value to fit into the size of an int, then it is type long.

By default, a floating-point number is type double.

Character constants are treated as small int values. For example, on some systems, the numeric value of 'A' is 65. The numeric values that are used to represent characters depends on the character set used by the system. [Some popular character sets are ASCII, EBCDIC, and Unicode.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Arithmetic Operators

All arithmetic operators are binary operators (i.e. they take two operands).

   *     multiply    
   /     divide
   %     modulus (remainder)
   +     addition
   -     substraction

Important points.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}


Home Previous Next