INFSCI 0015 Data Structures Lecture 1: Data Types - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 1: Data Types

Description:

INFSCI 0015 - Data Structures. Lecture 1: Data Types. Peter ... PC: Crimson Editor, PFE. Mac: BBEdit. Unix: emacs, vi, nedit. Compiler. PC: LCC. Unix: cc/gcc ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 22
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 1: Data Types


1
INFSCI 0015 - Data StructuresLecture 1 Data
Types
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/index.ht
    ml

2
Compilation and errors
  • The edit-compile loop
  • 1. Edit program
  • 2. Compile program
  • 3. If there are errors, go back to 1
  • you have got syntax error
  • 4. Run it
  • 5. If it produce wrong results go back to 1
  • you have got semantic error

3
Programming tools
  • Editor
  • PC Crimson Editor, PFE
  • Mac BBEdit
  • Unix emacs, vi, nedit
  • Compiler
  • PC LCC
  • Unix cc/gcc
  • IDE
  • LCC-Win, MS Visual C, Borland, Leonardo

4
Hello World - Compile Error
  • include ltstdio.hgt
  • main()
  • printf(Hello World!\n)
  • The semicolon is missing
  • What happens when we compile this?

5
Hello World - Compile Error
  • What happens when we compile this?
  • (5) unixs4 cc hello-nosemi.c
  • "hello-nosemi.c", line 6 syntax error before or
    at
  • (6) unixs4

6
Hello World - Experiment 2
  • include ltstdio.hgt
  • main()
  • printf(Hello World) /missing
    newline/
  • What happens when we run this?

7
Hello World - Experiment 2
  • What happens when we run this?
  • (13) unixs4 cc hello-nonewline.c
  • (14) unixs4 ./a.out
  • Hello World(15) unixs4

8
Hello World - Experiment 3
  • include ltstdio.hgt
  • main()
  • printf(Hello\n World\n) /extra
    newline/
  • What happens when we run this?

9
Hello World - Experiment 3
  • What happens when we run this?
  • (17) unixs4 cc hello-extranewline.c
  • (18) unixs4 ./a.out
  • Hello
  • World
  • (19) unixs4

10
From Data to Data Structures
machine level data storage
0100110001101001010001
primitive data types
28
3.1415
'A'
array
structure
data aggregates
high-level data structures
stack
queue
tree
11
On each level...
  • We do not want to be concerned with the way to
    represent objects of this level via objects of
    lower level
  • We want to be concerned with the semantics of
    data on this level.
  • What is it?
  • What we can do with it?

12
Primitive Data Types
13
Primitive Data Types
  • Integer data
  • 1, 10, 999, 1000
  • Floating point data
  • 3.1415, 0.001, 2.0
  • Characters
  • 'A', 'B', '_', '_at_'

10
999
3.1415
0.001
'_at_'
'A'
14
Primitive data can be printed
  • include ltstdio.hgt
  • main()
  • printf(Hello, World!\n)
  • printf(Here are integers d d\n, 10, 99)
  • printf(Here are floats f f\n, 3.1415,
    0.001)
  • printf(Here are characters c c\n, 'A',
    '_at_')

15
How printf works
  • Simple form - prints a string
  • printf("Some String\n")
  • Note special symbols \t and \n
  • Regular form
  • printf("..d..f..c..\n",
  • Expr1, Expr2, Expr3)
  • specifications should match expressions

16
Expressions?
  • What is an expression?
  • Anything that can have a value
  • In C almost anything is an expression
  • Expressions can have values of different type
  • A literal constant is an expression
  • 4 3.1415 99 'A' 0.0001

17
More control over printing
  • include ltstdio.hgt
  • main()
  • printf("Hello, World!\n")
  • printf("Integer 5d\n", 10)
  • printf("Float 4.2f\n", 3.1415)
  • printf("Characters c\n", 'A')
  • printf("Mix 08d 8.5f c\n", 99, 0.001, '_at_')

18
Summary of print control
  • d prints a decimal integer
  • 6d prints a decimal integer at least 6 char wide
  • f prints a floating point
  • 6f prints a floating point at least 6 char wide
  • .2f prints a floating point, 2 characters after
    decimal point
  • 6.2f prints a floating point at least 6 char
    wide and 2 characters after decimal point

19
We can calculate
  • include ltstdio.hgt
  • main()
  • printf("Let's calculate!\n")
  • printf("1234 4321 5d\n", 1234 4321)
  • printf("23.141510 7.1f\n", 23.141510)
  • printf("5/2 d\n", 5/2) / int division /
  • printf("5.0/2 f\n", 5.0/2)
  • / type conversion /
  • printf("52 d\n", 52) / mod operation /

20
Type conversion
  • Automatic type conversion
  • If operands are of the same type, the result will
    be of this type too
  • If operands are of different types, they will be
    converted to the broader type (I.e., int to
    float) before the calculation
  • Later we will learn about assignment conversion
    and direct conversion

21
Before next lecture
  • Complete your CourseWeb assignment (6pt)
  • Try Course static Web site
  • Check Syllabus
  • Install / try program development tools
  • Get and check one of the C books
  • Download, compile and run examples
Write a Comment
User Comments (0)
About PowerShow.com