Elements of a C Program - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Elements of a C Program

Description:

can be placed anywhere in the program. two methods for comments: /* ignore everything in between ... tells the compiler that your actual program starts here ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 10
Provided by: ianmar
Category:

less

Transcript and Presenter's Notes

Title: Elements of a C Program


1
Elements of a C Program
ECOR 1606 - Problem Solving and Computers
Carleton University Department of Systems and
Computer Engineering
2
Elements of a simple program
  • Comments
  • meant for the human reader
  • ignored by the compiler
  • can be placed anywhere in the program
  • two methods for comments
  • / ignore everything in between /
  • // ignore until the end of the line

3
Elements of a simple program
  • Include directives
  • include ltfilenamegt
  • tells compiler to read the specified header file
  • enables optional features
  • permits access to library functions
  • examples of standard header files
  • iostream cin, cout
  • cmath pow(), exp(), sin(), sqrt( )
  • cstdlib rand(), qsort(), getcwd( )

4
Elements of a simple program
  • Main function
  • tells the compiler that your actual program
    starts here
  • basically, this is the name of the function that
    the operating system calls to begin running your
    program
  • this function must be called main

5
Elements of a simple program
  • Declarations
  • specifies the names and types of variables that
    will be used
  • examples
  • int x
  • double y

6
Elements of a simple program
  • Executable Statements
  • the actual instructions for the algorithm
  • examples
  • cout ltlt "Hello, World!"
  • x 10 y
  • z sqrt(17.2)

7
Hello, World! Program
/ Print "Hello, World!", then exit.
/ include ltiostreamgt // for cout using
namespace std int main() cout ltlt "Hello,
World!" ltlt endl system("PAUSE") return
0
8
Average Value Program
comment
/ Read in three numbers and print their
average value / include ltiostreamgt using
namespace std int main() double
value1, value2, value3, average cout ltlt
"Enter three numbers " cin gtgt value1 gtgt
value2 gtgt value3 average (value1
value2 value3) / 3 cout ltlt "The
average is " ltlt average ltlt endl
system("PAUSE") return 0
include directives
declarations
main function
print prompt
read values
calculate average
print result
9
User Window
  • Program output (shown here in black) the
    content of the output stream produced by the
    statements of the form cout ltlt . . .
  • Program input (shown here in red) the content of
    the input stream entered by the user and
    processed by the statements cin gtgt . . .
Write a Comment
User Comments (0)
About PowerShow.com