Chapter 3 Structure of a C Program

Similar documents
Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Expressions and Precedence. Last updated 12/10/18

1.3b Type Conversion

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Type Conversion. and. Statements

LECTURE 3 C++ Basics Part 2

QUIZ: What value is stored in a after this

CHAPTER 3 Expressions, Functions, Output

Operators. Java operators are classified into three categories:

UNIT- 3 Introduction to C++

Operators & Expressions

C Programming

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Full file at

Full file at

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Information Science 1

Operators and Expressions:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Visual C# Instructor s Manual Table of Contents

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Programming in C++ 5. Integral data types

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Add Subtract Multiply Divide

Fundamentals of Programming Session 7

Programming for Engineers Iteration

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 2. Lexical Elements & Operators

Information Science 1

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Unit 3. Operators. School of Science and Technology INTRODUCTION

A First Program - Greeting.cpp

Basics of Java Programming


Chapter 3: Operators, Expressions and Type Conversion

Chapter 12 Variables and Operators

JAVA Programming Fundamentals

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Declaration and Memory

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Chapter 2: Using Data

Expressions and Casting

Structured programming. Exercises 3

Reserved Words and Identifiers

The New C Standard (Excerpted material)

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CIS133J. Working with Numbers in Java

Programming for Engineers Introduction to C

Computers Programming Course 6. Iulian Năstac

Programming for Engineers: Operators, Expressions, and Statem

Chapter 2 Basic Elements of C++

Section we will not cover section 2.11 feel free to read it on your own

Lecture 3. More About C

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Course Outline Introduction to C-Programming

Java Programming Fundamentals. Visit for more.

Chapter 2: Using Data

3. Java - Language Constructs I

UNIT 3 OPERATORS. [Marks- 12]

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Java enum, casts, and others (Select portions of Chapters 4 & 5)

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators And Expressions

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Prefix/Infix/Postfix Notation

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

C-LANGUAGE CURRICULAM

Chapter 4: Basic C Operators

Lecture 3 Operators MIT AITI

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

These are reserved words of the C language. For example int, float, if, else, for, while etc.

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

4. Inputting data or messages to a function is called passing data to the function.


9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 2: Data and Expressions

Chapter 2 Working with Data Types and Operators

Operators in C. Staff Incharge: S.Sasirekha

SECTION II: LANGUAGE BASICS

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Chapter 12 Variables and Operators

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Chapter 2: Data and Expressions

Transcription:

Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand the result of side effects in expression evaluation To be able to predict the results when an expression is evaluated To understand implicit and explicit type conversion To understand and use the first four statement types: null, expression, return, and compound Computer Science: A Structured Programming Approach Using C 1

3-1 Expressions An expression is a sequence of operands and operators that reduces to a single value. Expressions can be simple or complex. An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed; it receives an operator s action. Topics discussed in this section: Primary Expressions Postfix Expressions Prefix Expressions Unary Expressions Binary Expressions Computer Science: A Structured Programming Approach Using C 2

Note An expression always reduces to a single value. Computer Science: A Structured Programming Approach Using C 3

FIGURE 3-1 Expression Categories Computer Science: A Structured Programming Approach Using C 4

Primary Expressions Names a, b12, price, INT_MAX, SIZE Literal constants 5, 123.98, A, Welcome Parenthetical expressions (2 * 3 + 4), (a = 23 + b * 6) Computer Science: A Structured Programming Approach Using C 5

FIGURE 3-2 Postfix Expressions Computer Science: A Structured Programming Approach Using C 6

Note (a++) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C 7

FIGURE 3-3 Result of Postfix a++ Computer Science: A Structured Programming Approach Using C 8

Note The operand in a postfix expression must be a variable. Computer Science: A Structured Programming Approach Using C 9

PROGRAM 3-1 Demonstrate Postfix Increment Computer Science: A Structured Programming Approach Using C 10

PROGRAM 3-1 Demonstrate Postfix Increment (continued) Computer Science: A Structured Programming Approach Using C 11

Function Call A postfix expression Function call operator () printf( hello world\ n ) Computer Science: A Structured Programming Approach Using C 12

FIGURE 3-4 Prefix Expression Computer Science: A Structured Programming Approach Using C 13

Note (++a) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C 14

FIGURE 3-5 Result of Prefix ++a Computer Science: A Structured Programming Approach Using C 15

Note The operand of a prefix expression must be a variable. Computer Science: A Structured Programming Approach Using C 16

PROGRAM 3-2 Demonstrate Prefix Increment Computer Science: A Structured Programming Approach Using C 17

PROGRAM 3-2 Demonstrate Prefix Increment (continued) Computer Science: A Structured Programming Approach Using C 18

Note If ++ is after the operand, as in a++, the increment takes place after the expression is evaluated. If ++ is before the operand, as in ++a, the increment takes place before the expression is evaluated. Computer Science: A Structured Programming Approach Using C 19

FIGURE 3-6 Unary Expressions Computer Science: A Structured Programming Approach Using C 20

Table 3-1 Examples of Unary Plus And Minus Expressions Computer Science: A Structured Programming Approach Using C 21

FIGURE 3-7 Binary Expressions Computer Science: A Structured Programming Approach Using C 22

Binary Expressions 10 * 3, true * 4, A * 2, 22.3 * 2 10 / 3, true / 4, A / 2, 22.3 / 2 10 % 3, true % 4, A % 2, 22.3 % 2 3 / 5, 3 % 5 3 + 7, 3 7 a = 5, b = x + 1, i = i + 1 Computer Science: A Structured Programming Approach Using C 23

Note Both operands of the modulo operator (%) must be integral types. Computer Science: A Structured Programming Approach Using C 24

PROGRAM 3-3 Binary Expressions Computer Science: A Structured Programming Approach Using C 25

PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C 26

PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C 27

Note The left operand in an assignment expression must be a single variable. Computer Science: A Structured Programming Approach Using C 28

Table 3-2 Expansion of Compound Expressions Computer Science: A Structured Programming Approach Using C 29

PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C 30

PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C 31

PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C 32

3-2 Precedence and Associativity Precedence is used to determine the order in which different operators in a complex expression are evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression. Topics discussed in this section: Precedence Associativity Computer Science: A Structured Programming Approach Using C 33

PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C 34

PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C 35

Computer Science: A Structured Programming Approach Using C 36

a + b + c + d a * b * c * d FIGURE 3-8 Left-to-Right Associativity Computer Science: A Structured Programming Approach Using C 37

a = b = c = d = 10 FIGURE 3-9 Right-to-Left Associativity Computer Science: A Structured Programming Approach Using C 38

3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression on the right of the assignment operator and then places the value in the left variable. Changing the value of the left variable is a side effect. Computer Science: A Structured Programming Approach Using C 39

Side Effects x = 4; x = x + 4; a++ printf( x = %d\ n, x); printf( x = %d\ n, x = x + 4); scanf( %d, &x); Computer Science: A Structured Programming Approach Using C 40

Side Effects A side effect also means an unexpected change due to the use of a global variable You are advised not use a global variable! A clear example of this kind of side effect could be made when we have multiple functions Computer Science: A Structured Programming Approach Using C 41

3-4 Evaluating Expressions Now that we have introduced the concepts of precedence, associativity, and side effects, let s work through some examples. Topics discussed in this section: Expressions without Side Effects Expressions with Side Effects Computer Science: A Structured Programming Approach Using C 42

Expressions and Side Effects a * 4 + b / 2 c * b d = a * 4 + b / 2 c * b f = a * 4 + b++ / 2 c * --b Computer Science: A Structured Programming Approach Using C 43

PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C 44

PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C 45

PROGRAM 3-6 Evaluating Expressions Computer Science: A Structured Programming Approach Using C 46

Warning Computer Science: A Structured Programming Approach Using C 47

3-5 Type Conversion Up to this point, we have assumed that all of our expressions involved data of the same type. But, what happens when we write an expression that involves two different data types, such as multiplying an integer and a floating-point number? To perform these evaluations, one of the types must be converted. Topics discussed in this section: Implicit Type Conversion Explicit Type Conversion (Cast) Computer Science: A Structured Programming Approach Using C 48

Promotion Demotion FIGURE 3-10 Conversion Rank Computer Science: A Structured Programming Approach Using C 49

PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C 50

PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C 51

PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C 52

Explicit Type Conversion: Cast Cast Operator: (type-to-cast) operand Example int a=3; float b=4.0; b = (float)a; Computer Science: A Structured Programming Approach Using C 53

PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C 54

PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C 55

PROGRAM 3-8 Explicit Casts Computer Science: A Structured Programming Approach Using C 56

3-6 Statements A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions. You may have noticed that we have used a semicolon at the end of the statements in our programs. Most statements need a semicolon at the end; some do not. Topics discussed in this section: Statement Type The Role of the Semicolon Statements and Defined Constants Computer Science: A Structured Programming Approach Using C 57

FIGURE 3-11 Types of Statements Computer Science: A Structured Programming Approach Using C 58

Null Statement Just a semicolon Example ; // null statement They do nothing, but are still valid syntactical objects. Computer Science: A Structured Programming Approach Using C 59

Expression Statement A statement consisting of an expression Example expression; a = 2; b = c = 3; Computer Science: A Structured Programming Approach Using C 60

Return Statement A return statement terminates a function. Example return; return expression; Computer Science: A Structured Programming Approach Using C 61

FIGURE 3-12 Compound Statement Computer Science: A Structured Programming Approach Using C 62

Note The compound statement does not need a semicolon. Computer Science: A Structured Programming Approach Using C 63

The Role of the Semicolon Every declaration in C is terminated by a semicolon Most statements in C are terminated by a semicolon Computer Science: A Structured Programming Approach Using C 64

Statements and Defined Constants #define TAX_RATE 0.825; tax = TAX_RATE * amount; Computer Science: A Structured Programming Approach Using C 65

3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured Programming Approach Using C 66

PROGRAM 3-9 Calculate Quotient and Remainder Computer Science: A Structured Programming Approach Using C 67

PROGRAM 3-9 Calculate Quotient and Remainder Computer Science: A Structured Programming Approach Using C 68

PROGRAM 3-10 Print Right Digit of Integer Computer Science: A Structured Programming Approach Using C 69

PROGRAM 3-10 Print Right Digit of Integer Computer Science: A Structured Programming Approach Using C 70

PROGRAM 3-11 Calculate Average of Four Numbers Computer Science: A Structured Programming Approach Using C 71

PROGRAM 3-11 Calculate Average of Four Numbers Computer Science: A Structured Programming Approach Using C 72

PROGRAM 3-11 Calculate Average of Four Numbers Computer Science: A Structured Programming Approach Using C 73

PROGRAM 3-11 Calculate Average of Four Numbers Computer Science: A Structured Programming Approach Using C 74

PROGRAM 3-12 Convert Radians to Degrees Computer Science: A Structured Programming Approach Using C 75

PROGRAM 3-12 Convert Radians to Degrees Computer Science: A Structured Programming Approach Using C 76

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C 77

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C 78

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C 79

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C 80

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 81

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 82

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 83

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 84

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 85

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 86

PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C 87

3-8 Software Engineering In this section we discuss three concepts that, although technically not engineering principles, are important to writing clear and understandable programs. Topics discussed in this section: KISS Parentheses User Communication Computer Science: A Structured Programming Approach Using C 88

KISS (Keep It Simple and Short) Note Blocks of code should be no longer than one screen. Computer Science: A Structured Programming Approach Using C 89

Note Use parentheses whenever you are not sure of precedence. Computers do what you tell them to do, not what you intended to tell them to do. Make sure your code is as clear and simple as possible. Computer Science: A Structured Programming Approach Using C 90

PROGRAM 3-15 Program That Will Confuse the User Always provide the user with clear information printf( Enter two integers and key <return>\n ); Computer Science: A Structured Programming Approach Using C 91