Module 2 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Module 2

Description:

braces tell beginning or ending. return 0; //our first statement! ... braces is the function body. What Happens to the Comments? ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 18
Provided by: humb
Category:
Tags: braces | module

less

Transcript and Presenter's Notes

Title: Module 2


1
Module 2
  • Some Simple Programs

2
What is a C Program?
  • It is a collection of one or more functions
  • Functions act as organizing groups, each doing a
    smaller task and working together to produce the
    desired result
  • A C program must have a function called main()
  • Execution always begins with the first statement
    in main()
  • All other functions are subprograms and are
    executed only when called

3
Implication for writing software?
  • The natural way to organize your code is to have
    the main() function serve as an outline
  • Keep functions relatively small (one to two
    screen fulls)
  • Another term for function is methodI will use
    the term function, mainly, since it is consistent
    with our text

4
The Smallest C Program
// This is a commentit is ignored by the
compiler! int main( ) //function
header //braces tell beginning or
ending return 0 //our first statement!
//everything inside and including
the //braces is the function body
5
What is in a Function Header?
type of returned value
parameter listlist of inputs to function
int main ( )
name of the function
6
So what does this program do?
// This is a commentit is ignored by the
compiler! int main( ) //function
header //braces tell beginning or
ending return 0 //our first statement! //e
verything inside and including the //braces is
the function body
7
What Happens to the Comments?
  • The compiler works in several stages, including
  • Preprocessor
  • Syntax recognition
  • Semantics
  • Optimizer
  • One job of the preprocessor is to remove comments
  • The preprocessor also notes which libraries to
    load and takes care of defined macros

8
General Structure of C Programs
include ltstdio.hgt include myheaderfile.h defi
ne PI 3.1415927 using namespace std const type
identifier type identifier function function
function
9
Lets Start With Identifiers
  • An identifier is a name you give to a data object
    (variable or constant) or function
  • C is case sensitive
  • Use meaningful identifiers!

10
What does this function do?
int myfunction( int t, int v ) int t p t
v return p
11
How about this one?
// Function calculate_pressure calculates
pressure from // inputs temperature and volume
and returns the value // of pressure. int
calculate_pressure( int temperature, int volume
) int pressure pressure temperature
volume return pressure
12
Rules for Identifiers
  • All elements of C follow rigid rules
  • Remember, the computer is stupid!
  • Identifiers must start with a letter or
    underscore, which is followed by zero or more
    letters, digits, or underscores
  • Spaces not allowed they are delimiters
  • Oddball characters not allowed

13
Data Types
simple
structured
address
integral enum floating
array struct class union
pointer reference
char short long int
float double long double
unsigned
14
Standard Data Types for C/C
  • Integral Types
  • Represent integerspositive and negative values
    without fractions
  • Declared as int, short, or long eg 4578,
    -4578,0
  • Floating Types
  • Real numbers with a decimal point
  • Declared as float or double eg 95.33, 95.0, 0.0
  • Characters
  • Represent single characters
  • Declared as char eg B, d, ?,

15
Putting Identifiers with declarations yields
  • A variable is a location in memory which we refer
    to by the identifier it is given we can store
    data of the appropriate type here!
  • Declaring a variable means specifying both its
    name and its data type
  • Examples

int ageOfDog float taxRateY2K
16
What a Variable Declaration Does
  • A variable declaration tells the compiler to set
    aside enough memory for the item to be stored
  • Different variables require different amounts of
    memory
  • For example, the float might require four bytes
    and the integer two bytes

17
Note on Declarations
  • You can put more than one on a single line
  • This makes adding comments harder, so dont abuse
    this privilege

int money, monie, moonie
Write a Comment
User Comments (0)
About PowerShow.com