Precedence - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Precedence

Description:

function = a separate named block of statements that you call by name in order to execute ... #include 'X' == 'search current directory for file X' ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 10
Provided by: vdou8
Category:

less

Transcript and Presenter's Notes

Title: Precedence


1
Precedence associativity
  • How would you evaluate the expression 17 - 8 2
    ? Is it 17 - (8 2) or (17 - 8) 2 ?
  • These two forms give different results.
  • We need rules!

2
Precedence associativity
  • When two operators compete for the same operand
    (e.g. in 17 - 8 2 the operators - and compete
    for 8) the rules of precedence specify which
    operator wins.
  • The operator with the higher precedence wins
  • If both competing operators have the same
    precedence, then the rules of associativity
    determine the winner.

3
Precedence associativity
! Unary / lt lt gt gt
!
higher precedence
Associativity execute left-to-right (except for
and unary )
lower precedence
4
Precedence associativity
  • Examples X 17 - 2 8 Ans X17-(28) ,
    X1
  • Y 17 - 2 - 8 Ans Y (17-2)-8, Y7
  • Z 10 9 ((8 7) 6) 5 4 3 2 1 ?

Not sure? Confused? then use parentheses in your
code!
5
Statements?Functions
  • Big programs get complicated very fast. Small
    programs are simpler.
  • Key CS idea Decomposition
  • Break big complicated sets of instructions into
    smaller, simpler modules
  • In C, these modules are functions
  • function a separate named block of statements
    that you call by name in order to execute

a block of statements statement1
statement2 ...
6
A function...
  • ...is a "black box", a mini program with
  • a simple set of inputs
  • simple output
  • a body of statements
  • ...performs one well-defined task
  • (e.g. printf only prints to the screen)

7
Function essentials
  • A function has
  • a unique name
  • a body
  • some (optional) inputs (called arguments)
  • one (optional) output (called return value)
  • Just like variables, before a function can be
    used it must be
  • declared ( specify name, arguments, return value
    type)
  • defined ( write the body)

8
Functions you know already
  • main()
  • the first (and last) function executed when the
    program runs
  • printf(), scanf()
  • You can invoke (call) other functions from within
    main()
  • You can write your own functions which in turn
    may call other functions and so on.

9
How to use existing functions
  • Use include directives to tell C where to find
    the declarations of these functionsinclude
    ltstdio.hgt / includeltXgt "search the usual
    places for file X"/include "coatrack.h"/
    include "X" "search current directory for
    file X"/
  • Call the functions by name in your
    programs.scanf("d",numhats)isFull
    hatChek(hats)
Write a Comment
User Comments (0)
About PowerShow.com