CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

enum weekdays {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ... efficiency = low; case Tuesday: efficiency = normal; case Wednesday: ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 49
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 1
  • C Types and Control

2
Objectives
  • In this lecture, you will learn to
  • Write a simple C program
  • Use preprocessor and using directive
  • Display output to terminal
  • Define data with different types
  • Understand the syntactic difference between C
    and Java
  • Use typecasting and typedef

3
C vs. Java
  • High level differences
  • Compiled vs. interpreted
  • Security and robustness
  • Multithreading
  • API Differences

4
Reasons to Use C
  • Templates
  • Operator overloading
  • STL
  • Resource reclamation
  • Conditional compilation
  • Accessor and mutator
  • Multiple inheritance
  • Space efficiency

5
First C Program
6
First Program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout ltlt Hello World! ltlt endl
  • return 0

7
main Function
  • Control starts
  • Declared in global scope
  • Return type int
  • Parameters for command-line arguments

8
Preprocessor
  • Lines beginning with are preprocessor
    directives
  • include directive has the compiler insert
    source code from a system file, e.g. iostream or
    user-defined file

9
The using Directive
  • Is equivalent to import in Java
  • namespace is the C equivalent of packages

10
Output
  • cout is the standard output stream
  • ltlt is the overloaded output operator
  • What about input?

11
Basic Types
12
Integer
  • 16, 32, 64 bits, platform dependent
  • short/long, e.g. 10L
  • signed/unsigned, e.g. 10U
  • signed char

13
Floating Point
  • float
  • double

14
Character
  • 16bit unicode in Java, but no size specified in
    C standard
  • Signed or unsigned by default is unspecified.
  • Char constant is enclosed in
  • Escape sequence for unprintable characters

15
Boolean
  • bool true/false
  • Not originally part of C
  • Legal erroneous code

16
Enumerated Types
  • Defined by providing a range of constant values,
    e.g.
  • enum weekdays Monday, Tuesday, Wednesday,
    Thursday, Friday, Saturday, Sunday
  • Declaration of weekdays is as follows
  • weekdays workDays, VacationDays

17
local and global
  • Variables that declared outside of any function
    are called global
  • Accessible within all subsequent functions
  • Variables that are declared inside a function are
    local
  • Accessible within the bounds of the function
  • Or local to the body of a control structure

18
Arrays
  • A fixed-size set of same type values
  • Legal subscripts range from 0 to n - 1
  • The language does not check on the subscript
    index values
  • e.g.
  • int temp12
  • double matrix1020
  • string dayNames7 Monday, Tuesday,
    Wednesday, Thursday, Friday, Saturday,
    Sunday

19
Structures
  • A collection of fields of mixed data types
  • Represent data associated with a common subject
  • struct person
  • string name
  • int age
  • enum M, F gender

person me me.name zhou
20
Functions and Parameter Passing
21
Function Definition
  • C allows non-member functions.
  • Return type
  • Function name
  • Parameter list
  • body

22
Example 1
  • int bigger ( int a, int b)
  • return a gt b ? a b

23
Example 2
  • Compute the sum of the array elements
  • int aSum(int a, int n)
  • int sum 0
  • for ( int i 0 i lt n i)
  • sum ai
  • return sum

24
Local Variables
  • Local variables become active when function
    begins execution, disappear when the function
    finishes execution
  • A run-time stack is maintained for local
    variables
  • If function A invoke B, B must terminate before A
    resumes

25
Invocation
  • Provide arguments with types compatible with
    formal parameters
  • Pass-by-value
  • A copy of arguments is created
  • Actual argument values can not be changed
  • Pass-by-reference
  • A reference to the actual arguments is created
  • Argument values can be changed

26
Pass-by-Value
  • include ltiostreamgt
  • using namespace std
  • void swap ( int a, int b)
  • int tmp a
  • a b
  • b tmp
  • int main()
  • int x 1
  • int y 2
  • swap (x,y)
  • cout ltlt x ltlt x ltlt y ltlt y ltlt endl
  • return 0

27
Pass-by-Reference
  • include ltiostreamgt
  • using namespace std
  • void swap ( int a, int b)
  • int tmp a
  • a b
  • b tmp
  • int main()
  • int x 1
  • int y 2
  • swap (x,y)
  • cout ltlt x ltlt x ltlt y ltlt y ltlt endl
  • return 0

28
Declaration
  • In C, function must be defined before the
    invocation.
  • Or use prototype to declare functions earlier.
  • Not include body
  • Terminate with a semicolon.

29
Example
  • include ltiostreamgt
  • using namespace std
  • int bigger( int a, int b) // function
    declaration
  • int main()
  • int x 1
  • int y 2
  • cout ltlt bigger(x,y) ltlt is bigger ltlt endl
  • return 0
  • int bigger ( int a, int b)
  • return a gt b ? a b

30
Default Parameter
  • Specify default values for a formal parameter in
    the function definition.
  • All the subsequent parameters must have default
    values too.
  • Default values cannot be specified in both
    definition and declaration.
  • Good practice in the declaration

31
Example
  • int power (int b, int e 2)
  • Legal invocation
  • power(3) //return 9
  • power(3,3) //return 27
  • What about
  • int power (int e 2, int b)
  • Possible ambiguity
  • int power (int b, int e 2)
  • int power (int b)

32
C vs. JavaSyntactic Differences
33
Operators and Expressions
  • Java has well-defined semantics, whereas c code
    can be implementation dependant.

34
Case 1
  • int x 5
  • x x
  • In java, what is x?
  • In C?

35
Case 2
  • 8/-5 ?
  • In Java, integer division rounds down
  • In C, rounds down or rounds to the nearest

36
Case 3
  • C supports comma operator
  • Take two expressions, evaluate to the second
    expression. e.g. 2,5 evaluates to 5
  • Cause erroneous code 3.14 or 3,14
  • pow((3,4))?

37
Case 4
  • In Java, gtgtgt unsigned right shift, gtgt signed
    right shift
  • In C, only gtgt, depends on whether the left
    operand is signed or unsigned

38
Conditional Structures
  • if statement in C is identical to Java.
    However, in C the condition can be either an
    int or a bool
  • Cause legal erroneous code
  • if (x 0)
  • Historically, C interprets 0 as false and
    nonzero as true

39
Loops
  • C supports the for, while, and do loop with
    break and continue statements.
  • C does not allow labeled break and labeled
    continue

40
switch
  • Used to select one alternative out of many
  • Switch value must be integer, char or enumerated
    value
  • default can be used to issue default operations
    for non-matched values

41
Example
  • switch(aDay)
  • case Monday
  • efficiency low
  • case Tuesday
  • efficiency normal
  • case Wednesday
  • efficiency high
  • default
  • efficiency normal

42
Definite Assignment
  • Java makes sure every local variable has been
    assigned a value
  • C compilers are not required to check definite
    assignment
  • Note it wont take long till you encounter
    runtime error caused by uninitialized variables.

43
Other Syntax in C
  • Typecast
  • typedef statement

44
Typecast
  • C is more liberal
  • double z
  • int x 6
  • int y 10
  • z x/y
  • What is z?

45
typedef
  • Allow C programmer to assign meaningful names
    to existing types.
  • typedef short SMALLINT
  • typedef int BIGINT
  • SMALLINT i
  • BIGINT loop_count

46
Things to Remember
  • Controls start at main
  • Lines beginning with are preprocessor
    directives
  • include inserts source code from other files
  • Output to terminal cout ltlt
  • Primitive types are short, int, long, char
    (signed or unsigned)

47
Things to Remember
  • In C, zero represents false, non-zero is true.
  • C allows comma operator.
  • C does not support labeled break and continue.
  • C does not check definite assignment.
  • typedef may be used to replace existing types.

48
Possible Quiz Questions
  • What is include directive for?
  • What is the purpose of using?
  • Write a simple C program.
  • Use control structures if, loops. Pay attention
    to the true/false condition.
  • Difference between pass-by-value and
    pass-by-reference
  • Use typedef to replace an existing data type
Write a Comment
User Comments (0)
About PowerShow.com