Chapter 02 (Part II) - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Chapter 02 (Part II)

Description:

How to obtain information from the user. How to perform arithmetic calculations ... Reading variables from memory is nondestructive. Example: sum = number1 number2; ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 17
Provided by: tcu3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 02 (Part II)


1
Chapter 02 (Part II)
  • Introduction to C Programming

2
Goal
  • Example
  • How to obtain information from the user.
  • How to perform arithmetic calculations
  • Introduction to computer memory.

3
Obtaining Two Integers and Summing Them Up
4
Variables
  • We can store data in variables.
  • Variable must be declared with name and data type
    before used.
  • For example
  • int integer1
  • int integer2
  • Several variables of same type can be written in
    one declaration. For example,
  • int interger1, integer2, sum

Data type
Variable name
5
Principles of naming a Variable
  • The declaration of variables (in a block) cannot
    be duplicate.
  • Use identifiers of 31 characters or fewer to
    ensure portability.
  • Choosing meaningful identifiers.
  • Avoid identifiers that begin with underscores.
  • Always place a blank line between a declaration
    and adjacent executable statements.

6
Capturing Input
  • Input stream object
  • stdcin
  • Usually connected to keyboard
  • Stream extraction operator gtgt
  • Waits for user to input value until the user
    presses Enter.
  • Stores value in variable to right of the operator
  • Converts value to variable data type
  • Example
  • stdcin gtgt number1
  • Reads an integer typed at the keyboard
  • Stores the integer in variable number1

7
Adding integers
  • Assignment operator
  • Assigns value on right to variable on left
  • (correct) x 3
  • (error) 3 x
  • Binary operator (two operands)
  • lvalue rvalue
  • Example
  • sum integer1 integer2
  • Adding the values of integer1 and integer2
  • Storing result in sum
  • Remember the direction of assigning a value!
  • int x 3
  • int y x 2

8
Outputting the Result
  • Stream manipulator stdendl
  • Outputs a newline.
  • Flushes output buffer.
  • Concatenating stream insertion operations
  • Also called chaining or cascading.
  • Stream insertion operator knows how to output
    each type of data.
  • Example

sum number1 number2 stdcout ltlt "Sum is
stdcout ltlt sum stdcout ltlt stdendl
stdcout ltlt "Sum is " ltlt number1 number2 ltlt
stdendl
Outputs "Sum is Then, outputs sum of number1
and number2 Then, outputs newline and flushes
output buffer
9
Obtaining Two Integers and Summing Them Up
10
2.5 Memory Concept
  • Variable names
  • Correspond to actual locations (addresses) in
    computer's memory
  • Every variable has name, type, size and value

11
2.5 Memory Concept
  • A compiler maintain a table that maps variable
    names to real addresses, and their types (memory
    size).

45
72
12
2.5 Memory Concept
  • Variable names
  • When new value placed into variable, overwrites
    old value.
  • Writing to memory is destructive.
  • Reading variables from memory is nondestructive.
  • Example
  • sum number1 number2
  • Value of sum is overwritten.
  • Values of number1 and number2 remain intact.

13
2.6 Arithmetic
  • Arithmetic operators
  • Modulus operator returns remainder
  • 7 5 evaluates to 2
  • Attempting to use the modulus operator () with
    non-integer operands is a compilation error.

14
2.6 Arithmetic (Cont.)
  • Straight-line form
  • All constants, variables and operators appear in
    a straight line
  • Grouping subexpressions
  • Parentheses are used in C expressions to group
    subexpressions
  • Example
  • a ( b c )
  • Multiple a times the quantity b c

15
2.6 Arithmetic
  • Rules of operator precedence
  • Operators in parentheses evaluated first.
  • Multiplication, division, modulus applied next
  • Operators applied from left to right
  • Addition, subtraction applied last
  • Operators applied from left to right

16
Exercise
Write a Comment
User Comments (0)
About PowerShow.com