COSC 1315

Programming Fundamentals

Global main Function, Includes, Using, Code Blocks, Comments, and Simple Output

Revised:  May 21, 2008
By Richard G. Baldwin

File:  Pf00105
Practice Text


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Discussion

Source code versus machine code

You write your program in source code similar to that shown later in Listing 5.  Source code is a high level computer programming language that is understood by humans.

You then submit your source code to a compiler program that translates your source code into machine code.  Machine code is a low-level programming language that is understood by the computer.  Very few humans can understand machine code.

The source code in Listing 5

Listing 5 contains a very simple C++ program that illustrates the following programming elements:

Other than comments and code blocks, everything in the above list is peculiar to C++.

Mechanics for compilation and execution

You can use an IDE such as MS Visual Studio 6.0 or Dev C++ to compile and execute this program.

IMPORTANT:  In this course, we will be using a free set of tools known as Dev C++, version 5 or later.  This set of tools has been installed in the various ACC Computer Studies labs beginning with the Fall 2006 semester.

Program output

Once you compile the program, you can execute the program from within VS 6.0 or from within Dev C++.  When you execute from within VS 6.0, the following text will be displayed on the screen:

Hello World
Press any key to continue

If you execute the program in Listing 5 from within Dev C++, the output will appear on the screen momentarily and then disappear.

Using Dev C++

You can force the output to remain on the screen when executing from within Dev C++, by replacing the statement in Listing 5 that reads

return 0;
with the following two statements:

system("PAUSE");
return EXIT_SUCCESS;

IMPORTANT:  Because we will be using Dev C++ in this course, you should include the two statements shown above in all programs that you write for this course.

As of the Fall semester of 2006, a set of instructions for downloading and installing Dev C++ is available on Dr. Scholl's website at http://www.austincc.edu/jscholl/.  Select “Fundamentals of Programming COSC1315” from the list in GREEN, then select “Installing Dev C++”

You can find instructions for running Dev C++ by using the procedure shown above and selecting "Running Dev C++."

 

Program output from Dev C++

If you modify the program in Listing 5 as described above and then compile and execute it from within Dev C++, the output will be the same as shown for VS 6.0 above.

If you use the program named Dev C++ to compile the code in Listing 5, and run the program from the command prompt, the output will be:

Hello World

The text shown above will be followed by a new command prompt.

Output files

A successful compilation and execution from within VS 6.0 will produce ten new files plus one new directory named Debug.

A successful compilation and execution using Dev C++ will produce a single file named Includes01.exe.  It will not create a new directory named Debug.

Execution from the command prompt

After saving the code from Listing 5 in a file named Includes01.cpp and using the program named Dev C++ to compile the program, you can execute the program by entering either of the following commands at the command prompt:

Includes01.exe
Includes01

Will discuss in fragments

I will extract fragments from Listing 5 and highlight them in the paragraphs that follow.

The include and using compiler directives

The code fragment shown in Listing 1 shows the include and using compiler directives.  Listing 1 also shows some comments that I will discuss later in this document.  (If you prefer, you can skip forward to the Comments section and learn about comments at this point.)

//Note:  This is a comment.

//The following include compiler directive is
// required to cause library elements
// to be loaded that make it possible to do
// simple input/output in C++.
#include <iostream>

using namespace std;

Listing 1

These compiler directives are peculiar to C++.  Most C++ programs will contain one or more such compiler directives.

include

The purpose of the include compiler directive is to cause the system to find and to include the source code for various prewritten library functions in your source code before attempting to compile your program.

The code in Listing 1 causes a set of functions necessary to support standard input/output to be located and inserted into the program before an attempt is made to compile the program.  The set of functions is identified as:

<iostream>

using

Here is what one online resource has to say about the using directive:

"All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities."

Whether this makes sense to you or not, for now you will simply have to remember to follow this format for the programs that you write for the lab projects for this course.

The global main function

All C++ programs require a global function named main.  The main function is where execution of the program begins and ends.  The main function for our sample program is shown in Listing 2 with its body having been partially deleted for brevity.  (I will discuss the body elements later.)

//Every C++ program requires a global main
// function.  One syntax for the main function
// follows.  This is not the only syntax that
// can be used for the main function in C++.
int main(){//Note: Another single-line comment
  //body code partially deleted for brevity
 
  return 0;
}//end main

Listing 2

Syntax for the global main function

Note that the body of the method is surrounded by a matching pair of curly braces.  (You will learn later that this constitutes a block of code.)

The header or signature for the main function in C++ can be written in different formats.  The format shown in Listing 2, which returns a value of type int and takes no incoming parameters is one of those formats.  Note in particular:

main in other languages

Other languages such as Java and C# also require a similar main function, but it is normally called a method instead of a function in those languages.  Also the method is not global in Java and C#.

(I will explain the difference between global and non-global in a future lesson.)

Simple output using cout

This program uses a cout statement to display text on the computer screen.  The cout statement is in the main function, but that typically won't be the case in more complex programs.

The cout statement is shown in Listing 3.

     
      cout << "Hello World\n";

Listing 3

Unfortunately, the theory behind the use of cout is far too complex for you to understand at this point in your programming education, so you will just have to accept what I am about to tell you on faith.

Although this is not absolutely correct technically, for the purposes of this course you can think of cout as representing console output on the computer screen.

The statement in Listing 3 consists of several parts, which I will explain below.

Insertion operator, <<

You can think of the insertion operator (indicated by the symbols <<) as being capable of inserting everything to the right of itself into the text that appears on the screen.

Right operand of the insertion operator

Similarly, you can think of everything to the right of the insertion operator as the right operand of the insertion operator.

What is an operator?

An operator is a symbol, or combination of symbols (such as <<),  that operates on something to cause something to happen.

What is an operand?

The something that gets operated on is called an operand.

According to commonly used jargon, everything to the right of an operator is referred to as the right operand of the operator.  Everything to the left of the operator is commonly referred to as the left operand of the operator.

An operator commonly operates on it right and/or its left operands to make something happen.  In this case, the something that happens is to cause the right operand to be inserted into the text on the screen.

A string of characters

In the case of Listing 3, you can think of the characters between the quotation marks as characters that will be displayed on the screen (with one exception).

The newline character sequence

The exception is that you can think of the two-character sequence \n as not being displayed as characters.  Rather, in combination these two characters cause the screen cursor to go to the beginning of a new line on the screen.  This two-character sequence is often referred to as the newline escape character.

Code blocks

C++, Java, and C# code can be partitioned into code blocks, which may be nested inside other code blocks.

Two nested code blocks in this sample program are illustrated in Listing 4.

(At this point, I will simply identify the code blocks.  I will explain the operational significance of code blocks in subsequent lessons.)

int main(){
  //Everything inside this matching pair of
  // curly braces constitutes a block of
  // code.

   

    { //Begin nested code block
      //Everything inside this matching
      // pair of curly braces constitutes a
      // nested block of code with no purpose
      // other than to illustrate the nesting of
      // code blocks.

      //Display something on the screen
      cout << "Hello World\n";
   
    }//end nested code block

  return 0;
}//end main

Listing 4

Syntax for a block of code

A code block always begins with a { and ends with a }.  All of the code between the matching curly braces constitutes a code block.

(Note, for example, that the entire body of the main method constitutes one block of code.  This block of code contains a nested block of code in this sample program.)

Nested code blocks

Code blocks can be nested inside of other code blocks so long as care is taken to make certain that the beginning and ending curly braces occur in matching pairs.

For example, the following is correct nesting (color added for emphasis).

{ {{ }} }

The following is not correct nesting:

{{{ }}

Comments

Comments in a C++, Java, or C# program are meant to convey information to a human observer, or in some cases to special programs (such as documentation programs) that are intended to do something with them.

Comments are not intended to have any impact on the execution of the program, and in fact are completely ignored by the compiler.

Two kinds of comments are in common use by C++, Java, and C# programmers.

Multi-line comments

Multi-line comments appear as follows.  Note the use of the /* and */ character combinations to signal the beginning and the end of a multi-line comment.

/*
One or more lines of comments
can appear between
the comment indicators
Another comment line
*/

Single-line comments

Single-line comments, indicated by //, can begin anywhere on a line.  Everything from that point to the end of the line is a comment that is ignored by the compiler.  Two examples of single-line comments follow:

int main()//A single-line comment
//Another single-line comment

Listing 5 contains numerous comments, which are used to explain various aspects of the program.  The comments are also used in some cases in Listing 5 to explain the format of comments.

Sample Program

/*Note: This is the beginning of a multi-line
comment, surrounded by the multi-line comment
delimiters.

File:  Includes01.cpp

This is a very simple C++ program.  It
illustrates the following programming
elements in C++:

The include compiler directive
The global main function
Simple output using cout
Code blocks
Comments

Other than comments and code blocks,
everything in the above list is peculiar to C++.

To compile this program, use an IDE such as MS
Visual Studio 6.0.  You can then execute the
program from within VS 6.0.  When you do, the
following text will be displayed on the screen:

Hello World
Press any key to continue

A successful compilation and execution from
within VS 6.0 will produce ten new files plus
one new directory named Debug.

You can also execute the compiled program by
making Debug the current directory and entering
the following command at the command prompt:

Includes01

Note: This is the end of the multi-line comment*/

//Note: This is a single-line comment.

//Note, the following compiler directive is
// required in order to cause library elements
// to be loaded that make it possible to do
// simple input/output in C++.
#include <iostream>
using namespace std;

//Every C++ program requires a global main
// function.  One syntax for the main function
// follows.  This is not the only syntax that
// can be used for the main function in C++.
int main(){//Note: Another single-line comment
  //Everything inside this matching pair of
  // curly braces constitutes a block of
  // code.

    //Everything inside the following matching
    // pair of curly braces constitutes a
    // nested block of code with no purpose other
    // than to illustrate the nesting of code
    // blocks.
    {//Begin nested code block
      //Display something on the screen
     
      cout << "Hello World\n";
    }//end nested code block
  return 0;
}//end main

Listing 5

-end-


Copyright 2005, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-