C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.

Slides:



Advertisements
Similar presentations
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Advertisements

Lecture 2 Introduction to C Programming
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 4: Basic C Operators
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expressions can be much more complex.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSCE 206 Structured Programming in C
CSE 220 – C Programming Expressions.
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
Operators And Expressions
Chap. 2. Types, Operators, and Expressions
Chapter 3 Assignment and Interactive Input.
Basic Elements of C++.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Lecture 3 Expressions Richard Gesick.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
A First Book of ANSI C Fourth Edition
Introduction to C Programming
Associativity and Prescedence
Chapter 4: Expression and Operator
Review of Previous Lesson
Lexical Elements & Operators
An Overview of C.
Operator King Saud University
Introduction to C Programming
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Presentation transcript:

C Programming Lecture 5

Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions are operated. In the expression * 3, the operator * has higher precedence than +, causing the multiplication to be performed first.In the expression * 3, the operator * has higher precedence than +, causing the multiplication to be performed first. The result is 7 instead of 9.The result is 7 instead of 9.

Associativity of Operators b When two operators placed in proximity in an expression have the same precedence, their associativity is used to determine how the expression is evaluated. In the expression 6 / 2 * 3, both / and * have the same precedence. Since they both have left to right associa- tivity, the expression has the value 9 rather than 1.In the expression 6 / 2 * 3, both / and * have the same precedence. Since they both have left to right associa- tivity, the expression has the value 9 rather than 1.

Partial Table of Operator Precedence and Associativity Operator Associativity () ++ (postfix) -- (postfix) left to right +(unary) -(unary) ++(prefix) --(prefix) right to left * / % left to right + - left to right = += -= right to left Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Parentheses and the Order of Operations b Expressions inside parentheses are evaluated first. This provides for the use of parentheses to clarify or change the order in which operations are performed.This provides for the use of parentheses to clarify or change the order in which operations are performed * 3 has a value of 7. (1 + 2)* 3 has a value of 9. (1 + 2)* 3 has a value of 9.

Binary Plus versus Unary Plus b Both binary plus and unary plus are represented by a + (plus sign). The same is true of binary and unary - (the minus sign).The same is true of binary and unary - (the minus sign). b Unary + and - have a higher precedence that binary + and - and the unary operators associate right-to-left instead of left-to-right.

Example of Unary Operators b In the expression - a * b - c the first minus is unary and the second is binary. b We can use parentheses to write an equivalent expression that is less likely to be misinterpreted. ((- a) * b) - c ((- a) * b) - c

Example of Unary Operators Using Numbers -1 * has a value of * has a value of -5 it is equivalent to ((-1) * 2) - 3 or (-2) - 3 ((-1) * 2) - 3 or (-2) - 3 which is -5 which is -5 it is not equivalent to (-1) * (2 - 3) or (-1) * (-1) (-1) * (2 - 3) or (-1) * (-1) which is +1 which is +1

Increment and Decrement Operators b ++ (the increment operator) and -- (the decrement operator) are unary operators with the same precedence and right-to-left associativity as the other unary operators. b The ++ and -- operators can occur in either a prefix or postfix position with different results.

Prefix versus Postfix When Using Increment and Decrement Operators b Each of the expressions ++i and i++ causes the stored value of i to be incremented by 1, however: The expression ++i causes the stored value of i to be incremented first, with the expression then taking as its value the new stored value of i.The expression ++i causes the stored value of i to be incremented first, with the expression then taking as its value the new stored value of i. The expression i++ has as its value the current value of i; then the stored value is incremented.The expression i++ has as its value the current value of i; then the stored value is incremented.

Example of the Increment and Decrement Operators int a, b, c = 0; a = ++c; b = c++; printf(“%d %d %d\n”, a, b, ++c); /* is printed */ c is incremented making its value 1. The result assigned to a making its value 1. The value of c is assigned to b making its value 1. Then c is incremented making its value 2. Finally, c is incremented before it is printed, making its value 3.

Practice with Operators and Expressions Declarations and Initializations int a = 1, b = 2, c = 3, d = 4; Expression Equivalent expression Value a * b / c (a * b) / c 0 a * b % c + 1 ((a * b) % c) a * b - c -- ((++a) * b) - (c--) b * ++d 7 - ((-b) * (++d)) 17

Partial Table of Operator Precedence and Associativity Operator Associativity () ++ (postfix) -- (postfix) left to right +(unary) -(unary) ++(prefix) --(prefix) right to left * / % left to right + - left to right = += -= right to left Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Assignment Operators b C treats = as an operator. It’s precedence is lower than almost all of the other operators.It’s precedence is lower than almost all of the other operators. It’s associativity is right to left.It’s associativity is right to left.

Form of an Assignment Expression b A simple assignment expression is of the form variable = right_side b The value of right_side is assigned to variable, and that becomes the value of the assignment expression as a whole.

Assignment Expressions versus Assignment Statements b An assignment expression has no semicolon at the end. An assignment statement does.An assignment statement does. b We can use assignment expressions to condense a sequence of assignment statements.

Example of Equivalent Code Using Assignment Expressions b Assignment statements b = 2; c = 3; a = b + c; b Equivalent statement using assignment expressions a = (b = 2) + (c = 3); Note the assignment statement ends with a semicolon, the expressions don’t.

Other Assignment Operators b C has operators that combine assignment with other operations. These are considered assignment operators and have the same precedence and right-to-left associativity as =. b Example k = k + 2; is equivalent to k += 2;

The Assignment Operators = += -= *= /= %= >>= >= <<= &= ^= |= The semantics of the other assignment operators is specified by variable op= expression which is equivalent to variable = variable op (expression) j *= k + 3 is equivalent to j = j * (k + 3) and not j = j * k + 3

Preprocessor Directives b Include files (header files) #include #include –Causes the preprocessor to look for filename in system defined places and replace the #include line with a copy of contents of filename. #include “filename”#include “filename” –Same as above, but the preprocessor looks in the current directory before looking in the system defined directory locations.

Where are these “System Defined” Places? b On UNIX systems, the standard header files such as stdio.h can typically be found in the directory /usr/include. You can use any editor to look at what is in these files.You can use any editor to look at what is in these files.

Header Files Must be Included for Functions in the Standard Library b The system knows where to find the code for functions in the standard library. b However, it is the responsibility of the programmer to include the header files that provide the prototypes for library functions.

The Libraries versus the Header Files b The standard library contains object code (already compiled library functions). b The standard header files are text files that you can read using a text editor. They provide information needed by the library functions.

Style b Do not condense code just for the sake of using less space y = 2; y = 2; z = 3; z = 3; x = y + z; x = y + z; is more readable than x = (y = 2) + (z = 3); x = (y = 2) + (z = 3);and a += 7; versus a = a + 7; a += 7; versus a = a + 7; is a matter of choice.

Common Programming Errors b Warning and Error Messages usually refer to a line number. The problem may be prior to that line -- such as not properly closing a comment or a string constant.The problem may be prior to that line -- such as not properly closing a comment or a string constant.

System Considerations b ANSI C has both a unary + and a unary - Traditional C has only unary -, so you should not use the unary + operator if your are writing code that needs to be portable to a machine that uses traditional C.Traditional C has only unary -, so you should not use the unary + operator if your are writing code that needs to be portable to a machine that uses traditional C. b If you are writing code for a spectrum of systems, limitations of all of the systems must be respected.