Program Construction in C - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Program Construction in C

Description:

CS 1284 Introduction to Computer Programming. Hello World // A first program to display Hello ... However, line breaks are not allowed inside of quotations ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 16
Provided by: bridges
Category:

less

Transcript and Presenter's Notes

Title: Program Construction in C


1
Program Construction in C
  • Reading Chapter 2

2
Hello World
  • // A first program to display Hello world
  • include ltiostreamgt // preprocessor directive
  • using namespace std
  • int main() // Beginning of function body
  • cout ltlt "Hello World!" ltlt endl
  • return 0
  • Output
  • Hello World!

3
Structure of a C program
  • A C program is a collection of one or more
    functions.
  • There must be a function called main().
  • Program execution always begins with the first
    statement in function main().
  • Any other functions in the program are not
    executed until they are called.

4
Example
  • include ltiostreamgt // preprocessor directive
  • using namespace std
  • define PI 3.14 // preprocessor directive
  • int main() // Function heading
  • // Beginning of function body
  • int radius // Variable declarations
  • float area
  • radius 5 // Assignment statements
  • area PI radius radius
  • cout ltlt "The radius is " ltlt radius ltlt endl
  • cout ltlt "The area is " ltlt area ltlt endl
  • return 0
  • // End of function body

5
Example (Cont..)
  • Output
  • The radius is 5
  • The area is 78.5

6
Basic elements of a C program
  • Whitespaces
  • Directives
  • Comments
  • Word Symbols
  • Special Symbols
  • Variables/Constants
  • Statements
  • Functions

7
Whitespaces
  • Whitespaces are spaces, tabs, line endings, and
    blank lines
  • Irrelevant to the compiler
  • Should be used in specific places to make your
    program more readable

8
Directives
  • Directives tell the preprocessor to do things
  • Always begin with
  • End at the end of the source line
  • The include directive instructs the preprocessor
    to insert the contents of a particular header
    file
  • include ltiostreamgt
  • include ltstringgt
  • The define directive is used to define constants
  • define PI 3.14
  • define MAX_GRADE 100

9
Comments
  • Preceded by //
  • Has to be ended in the same line
  • C style allows comments to be written between /
    and /
  • Can extend over multiple lines
  • Ignored by the compiler
  • Very important to make a program understandable

10
Word Symbols
  • Also called reserved words or keywords
  • Part of the C language
  • Appendix A (p. 1443) in textbook

11
Special Symbols
  • Mathematical symbols
  • - /
  • Grammar symbols
  • . ? ,
  • Symbols made from 2 characters
  • lt ! gt ?

12
Identifiers
  • An identifier is the name used for a data object
    (variable/constant) or for a function.
  • The identifiers are case sensitive.
  • Using meaningful identifiers is a good practice.

13
Variables/Constants
  • Variables
  • Used to contain values that can be changed during
    the program execution
  • Constants
  • Used to contain values that can not be changed
    during the program execution

14
Functions
  • A function is a program module that performs some
    particular task.
  • A function has a name that can be used to
    reference (or call) the function.
  • The function name is preceded by the return type.
  • The function name is followed by the argument
    list in parentheses (sometimes there will be no
    arguments and so the parentheses will be empty).

15
Statements
  • A statement is an instruction that performs a
    specific operation.
  • radius 5
  • Every single statement ends with a semicolon ()
    and may extend over multiple lines.
  • Where the line ends is usually not important
  • However, line breaks are not allowed inside of
    quotations
Write a Comment
User Comments (0)
About PowerShow.com