ENERGY 211 CME 211 - PowerPoint PPT Presentation

About This Presentation
Title:

ENERGY 211 CME 211

Description:

char (ASCII characters), bool (Boolean values, true or false), integer types. 11 ... The data stored in this byte is the ASCII code of the corresponding character ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 23
Provided by: lamb8
Learn more at: https://web.stanford.edu
Category:
Tags: cme | energy | ascii

less

Transcript and Presenter's Notes

Title: ENERGY 211 CME 211


1
ENERGY 211 / CME 211
  • Lecture 3
  • September 26, 2008

2
What's in a C Program?
  • A program consists primarily of the declarations
    and definitions of its entities
  • Functions for performing operations
  • Variables for storing data
  • A declaration includes a name and type
  • The definition of an entity includes its
    declaration, but more importantly, it describes
    its code and data

3
Variables and Functions
  • A variable is a named memory location
  • Its type indicates the nature of the data stored
    in this memory location
  • A function consists of
  • The declaration of its name and type
  • Zero or more statements
  • A function's type consists of the types of its
    input arguments, and the type of its return value

4
Statements
  • Statements describe the operations performed by a
    function
  • Types of statements
  • Variable declarations or definitions
  • Expressions
  • Flow-control statements
  • Compound statements (enclosed in )
  • Non-compound statements must be terminated with a
    semicolon

5
A Simple Program
  • include ltiostreamgt // std objects defined in
    iostream
  • int main()
  • int i // i is an integer
  • int largest(0) // largest is initially zero
  • do // following statements are to be
    repeated
  • stdcout ltlt "Enter a positive number "
  • stdcin gtgt i // read integer, store in
    i
  • if (i lt 1)
  • break // terminate repetition
  • if (i gt largest)
  • largest i // update largest value
  • while (true) // repeat forever
  • stdcout ltlt "Largest is " ltlt largest ltlt
    stdendl
  • // print largest value and exit

6
Variable Definitions
  • The statements
  • int i // i is an integer
  • int largest(0) // largest is initially zero
  • declare that i and largest are variables of type
    int (for integer) and define both (i.e. allocate
    storage)
  • The (0) initializes the value to zero
  • Initialization is not required
  • Unlike MATLAB, the type of a variable must be
    declared before it is used

7
Scope
  • A variable defined within a compound
  • statement is unknown outside of it
  • A variable declared outside of any
  • function is at file scope, and known to
  • any function within the file
  • A variable declared with the extern
  • qualifier is declared, not defined (unless
  • initialized), can be used in several files
  • A variable can only be defined once
  • within a given scope

8
What is a Type?
  • A type is a specification for entities that have
    specified behavior
  • Categories of types
  • Value two instances with the same data are
    interchangeable
  • Object each instance has its own identity
  • For now, we only discuss value types
  • Instances of value types can exist as pure
    values, called literals

9
Examples of Literals
  • Character 'a', '\n' (newline), '\223' (ASCII
    code for ô, in octal)
  • String "Hello"
  • Boolean true or false
  • Integer
  • Decimal 0, 2, -3
  • Octal 040 ( 32 in decimal)
  • Hexadecimal 0xA5 ( 165 in decimal)
  • Floating-point -1.0, 1e-4, .14E5

10
Fundamental Types
  • Fundamental types are value types on which all
    other types are built
  • All fundamental types in C are either integral,
    or floating-point
  • Integral types
  • char (ASCII characters),
  • bool (Boolean values, true or false),
  • integer types

11
Signed Integer Types
  • One bit used to indicate sign
  • short
  • at least 2 bytes (16 bits)
  • Supports values from 32,767 to 32,767
  • int
  • "natural" integer type for given hardware
  • usually 4 bytes (32 bits)
  • long
  • at least 4 bytes
  • Supports values from 2,147,483,647 to
    2,147,483,647

12
Unsigned Integer Types
  • unsigned short
  • At least 2 bytes
  • Supports values from 0 to 65,535
  • unsigned int
  • Usually 4 bytes
  • unsigned long
  • At least 4 bytes
  • Supports values from 0 to 4,294,967,295

13
Names in C
  • The names given to variables and functions are
    known as identifiers
  • Identifiers contain letters, digits, underscores
  • Must begin with letter or underscore
  • Any length is allowed
  • Identifiers are case-sensitive!
  • Identifiers in simple program main, i, largest,
    std, cout, cin
  • Cant use keywords! (see p. 47 in text)

14
Beware of Bad Users
  • The preceding program expects the user to input
    integers
  • The gtgt operator, followed by an integer variable,
    assumes an integer is followed by a space or
    newline character (so it knows when to stop)
  • gtgt knows to skip over "white space"
  • What if the user types something else?
  • In this case, stdcin has the bool value false

15
Handling Bad Input
  • int main()
  • try // attempt to run the following code
  • int i int biggest(0)
  • do
  • stdcout ltlt "Enter a positive
    number "
  • stdcin gtgt i
  • // bad input, report failure
  • if (stdcin false)
  • throw stdexception()
  • if (i lt 1) break
  • if (i gt biggest) biggest i
  • while (true)
  • stdcout ltlt "Largest is " ltlt biggest ltlt
    '\n'
  • // end of try block
  • catch (...)
  • // any exception sends control to this
    point
  • stdcerr ltlt "Exception thrown"
  • // end of catch block

16
Graceful Failure
  • int i
  • int biggest(0)
  • do
  • stdcout ltlt "Enter a positive number "
  • stdcin gtgt i
  • if (stdcin false)
  • // any code in this try block, after the
    throw,
  • // will not be executed, so we should
    print out
  • // the largest value now
  • stdcout ltlt "Largest is " ltlt biggest ltlt
    '\n'
  • throw stdexception()
  • if ( i lt 1 ) break
  • if ( i gt biggest ) biggest i
  • while ( true )
  • stdcout ltlt "Largest is " ltlt biggest ltlt '\n'

17
Characters
  • A variable of type char is stored in a single
    byte (8 bits)
  • The data stored in this byte is the ASCII code of
    the corresponding character
  • The value zero denotes a null character, which
    marks the end of a string
  • Literals for special characters, such as
    non-printing characters, are specified using
    escape sequences (like \n for newline) or the
    ASCII code in octal

18
Reading a Line of Text
  • include ltexceptiongt
  • include ltiostreamgt
  • int main()
  • try
  • char c int count(0) // initially count
    is zero
  • stdcout ltlt "Type a line." ltlt stdendl
  • do // repeat the following
  • c stdcin.get() // read
    character
  • if ( stdcin false ) throw
    stdexception()
  • if ( c '\n' ) break // quit on
    newline
  • count // update count of
    characters entered
  • stdcout ltlt c // echo character
  • while ( true )
  • stdcout ltlt "You typed " ltlt count ltlt "
    characters\n"
  • catch (...)
  • stdcerr ltlt "Exception thrown"

19
Floating-point Types
  • float
  • usually 4 bytes
  • At least 6 significant digits of precision
  • Not a good choice unless space is limited
  • double
  • usually 8 bytes
  • At least 10 digits of precision
  • Should almost always be used
  • long double
  • Usually 10 bytes
  • Not supported by many C compilers

20
First Floating-Point Program
  • include ltexceptiongt
  • include ltiostreamgt
  • include ltcmathgt // needed to use math functions
  • int main()
  • try // initially total 0.0, value is
    uninitialized
  • double total(0.0) double value
  • do
  • stdcout ltlt "Type in a number lt
    9999. "
  • stdcin gtgt value
  • if ( stdcin false ) throw
    stdexception()
  • if ( value gt 9999.0 ) break
  • total value value // sum of
    squares
  • while ( true )
  • // print length of vector of values,
    which is the
  • // square root of the sum of squares
  • stdcout ltlt "Length is " ltlt sqrt(total)
    ltlt '\n'
  • catch (...) stdcerr ltlt "Exception
    thrown\n"

21
More About Literals
  • Integer literals can have suffixes
  • L or l to be interpreted as a long
  • U or u to be interpreted as unsigned
  • Floating-point literals followed by f or F are of
    type float rather than double, or by l or L for
    extended precision
  • String or character literals preceded by L are
    stored as wide-character strings (2-4 bytes each)

22
Next Time
  • Probably finishing these slides because you asked
    too many questions P
  • Ok, its really because I had too many slides
  • Arrays
Write a Comment
User Comments (0)
About PowerShow.com