Program Structure - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Program Structure

Description:

int main() specifies the name of a function called main. ... will typically return a value of 0. If an error in the program does occur (e.g. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 12
Provided by: adria9
Category:

less

Transcript and Presenter's Notes

Title: Program Structure


1
Program Structure
2
Hello World
Description of the program
filename
  • /
  • HELLO.CPP
  • Author Adrian Chan
  • Student ID 100123456
  • This program is a simple program that displays
    Hello World.
  • Modifications
  • 08/08/07 AC First created.

  • /
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout ltlt "Hello world!" ltlt endl // Display
    message

Name and student ID
List of modification. Date, (yy/mm/dd), initials
of who modified the file, and comment on the
modification. In this course, since you are not
problem going to do any modifications once the
program it working, it will just have one line as
shown here.
Program comment header
3
Comments
  • Programmers use comments in their code to provide
    information to the reader on what the code is
    doing. When a program is compiled, the compiler
    simply skips all comments. It is common (and
    good) practice to use a comment at the top of the
    code containing general information about the
    file, which is known as the header information.
  • A comment is indicated using the deliminators /
    and /. An alternative syntax is available for
    single line comments using a //. This indicates
    that the text following the // on this line is a
    comment. An end deliminator is not needed in this
    case.

4
include
  • The include directive tells the compiler that
    additional features are required in this program,
    which are not part of this file. In hello.cpp we
    have the directive
  • include ltiostreamgt
  • The compiler knows that it must also use the
    features that are specified in the library
    iostream. By including iostream in hello.cpp we
    are able to use the cout feature to display our
    messages.
  • Commonly used library files and examples of their
    features
  • iostream cin, cout
  • cmath pow(), exp(), sin(), sqrt()
  • cstdlib rand(), qsort(), getcwd()

5
main
  • int main() specifies the name of a function
    called main. C programs begin their execution
    by calling the main function. Functions are
    specified using the following syntax
  • ltoutput parametersgt ltfunction namegt(ltinput
    parametersgt)
  • For the purposes of this course, the main
    function will always be specified as int main().
    This means it has no inputs from the operating
    system, hence nothing specified in the
    parentheses. It also means the main function will
    be returning an integer to the operating system
    this will be discussed at a later slide.
  • The contents of a function are deliminated by
    and .

6
cout
  • A simple way of displaying information on the
    monitor is using cout. The code
  • cout ltlt "Hello world!" ltlt endl
  • displays Hello world! on the monitor followed by
    an end of line character (similar to a Carriage
    Return key on a typewriter it brings the cursor
    to the beginning of the next line). Text is
    deliminated using quotation marks "". The ltlt
    operator is used to indicate successive inputs to
    cout.

7
semicolon
  • The semicolon at the end of the cout statement is
    used as a statement terminator. Each individual
    statement must be ended with a semicolon. This
    concept is similar to a period in the English
    language, which is used to terminate each
    sentence.

8
System Pause
  • The system("pause") is used to have the program
    output
  • Press any key to continue . . .
  • which will wait for the user to press a key to
    continue the execution of the program. It is used
    at the end of a program to stop the output window
    from closing before one is able to read the
    contents of the output window.

9
Return
  • The return statement is used to return a value
    from a function. When the return statement is
    encountered, the function will exit. In the case
    of the main function, the program will stop
    (exiting the main function is equivalent to
    stopping the program). A return statement can
    return at most one value.
  • For the main function, the return statement will
    be returning an integer value back to the
    operating system. This value will not be used for
    any purpose in this course. If the program
    terminates with no errors occurring, we will
    typically return a value of 0. If an error in the
    program does occur (e.g. cannot open an input
    file causing the program to terminate early) the
    program will return a non-zero value in this
    course we will typically just use a value of -1
    in such cases.

10
Code Formatting
  • During compilation, white space (i.e. spaces,
    tabs, carriage returns) is ignored. The file
    hello.cpp could also be written as
  • include ltiostreamgt
  • using namespace stdint main()
  • cout ltlt "Hello world!" ltlt endl // Display
    message
  • system("pause")return 0
  • It is the deliminators, including the semicolon,
    that the compiler uses to distinguish the
    components of the code.
  • It should be readily apparent that the original
    code is much easier to read than the code shown
    on this slide. While the two programs function
    exactly the same, having properly formatted code
    helps to program efficiently, preventing mistakes
    and making it easier to track mistakes. Do not
    wait until your program is complete to format
    your code format properly as you create your
    code.

11
Problem 1 Hello world!!!
Write a program that displays the output shown
below.
  • Hello world!!!
  • This course is ECOR1606.
  • This course is great!
Write a Comment
User Comments (0)
About PowerShow.com