C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.

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

Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
Chapter 4: Basic C Operators
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
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 C Programming Chapter 2 : Data Input, Processing and Output.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
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.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
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.
Operators & Expressions
CSE 220 – C Programming Expressions.
Lecture 3 Java Operators.
Operators And Expressions
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Operators and Expressions
Lecture 3 Expressions Richard Gesick.
All the Operators 22-Nov-18.
More about Numerical Computation
All the Operators 4-Dec-18.
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Chapter 3 Operators and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Chapter 4: Expression and Operator
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
OPERATORS in C Programming
Presentation transcript:

C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang

Expressions and Statements Expression Combination of constants,variables,operators, and function calls Ex) a+b 3.0*x – tan(angle) Statement An expression terminated with a semicolon Ex) sum = x + y + z; printf(“Dragons!”);

Assignment Operator The equal sign = is an assignment operator Used to give a variable the value of an expression Ex) x=34.8; sum=a+b; slope=tan(rise/run); midinit=‘J’; j=j+3; x=y=z=13.0; Initialization Ex) int i=0;

Arithmetic operators Binary operators Addition : + Subtraction : - Multiplication : * Division : / Modulus : % // only works for integers values Unary operators +, - Integer division 1/2 = 0 (?), 3/2 = 1 (?)

Arithmetic operators In binary operators If two operands are int type : the result is int type If one or two operands are floating-point type : the result is floating-point type ⇒ = /5 ⇒ 12.0/5.0 = 2.4

increment/decrement Increment operator ++ i=i+1; i++; // postfix form ++i; // prefix form decrement operator – i=i-1; i--; // postfix form --I; // prefix form Difference between i++ and ++i ?

prefix vs. postfix Difference shows up when the operators are used as a part of a larger expression ++k : k is incremented before the expression is evaluated. k++ : k is incremented after the expression is evaluated. Ex) difference? int a; int i=0, j=0; a= (++i) + (++j); int b; int i=0, j=0; b= (i++) + (j++);

Shorthand Operators General syntax variable = variable op expression; is equivalent to variable op= expression; Common forms +=, -=, *=, /=, %= Examples j=j*(3+x); j *= 3+x; a=a/(s-5); a /= s-5;

Precedence, Associativity of Operators Operator Precedence determines the order in which operations are performed operators with higher precedence are employed first. Operator Associativity if two operators in an expression have the same precedence, associativity determines the direction in which the expression will be evaluated. *, /, % : L -> R +, - (bin): L -> R = : R -> L +, - (unary) : R -> L precedenceoperators 1 st unary +, unary - 2 nd binary * / % 3 rd binary + -

Precedence Examples Evaluation Order * > > >3 use parenthesis to force a desired order of evaluation Ex) (1 + 2) * (3 – 4)

Associativity Examples Left associativity a / b * c  (a / b) * c Right associativity a  - (+ (- a))

Bitwise Operators shift/lo gic Op. nameusagetypeoutput shift op. left shift a<<n integer Shift bits of a to left by n bit Newly created bits will be 0 right shift a>>n integer Shift bits of a to right by n bit Newly created bits will be 0 bit op. bit AND a & b integerAND of a ’s and b ’s each bit bit OR a | b integerOR of a ’s and b ’s each bit bit XOR a ^ b integerXOR of a ’s and b ’s each bit 1’s complement ~a integer1’s complement of a

Truth/False Table aba & ba | ba ^ b a~a 01 10

Bitwise Operators Examples 11 = = << << 2 = = >> >> 3 = = 2 output: 11 << 2 = >> 3 = 2

example output: 1f05 & 31a1 = f05 | 31a1 = 3fa5 1f05 ^ 31a1 = 2ea4 ~1f05 = ffffe0fa

example expressionvalueresult a0x1f b 0x31a ~a0xe0fa a & b 0x a | b 0x3fa a ^ b 0x2ea

Relational Operators meaning 연산자자료형결과값 Equal a == b integer or floating point 1(=true) if a is equal to b otherwise 0(=false) not equal a != b integer or floating point 1(=true) if a is not equal to b otherwise 0(=false) less than a < b integer or floating point 1(=true) if a is less than b otherwise 0(=false) less than or equal to a <= b integer or floating point 1(=true) if a is less than or equal to b otherwise 0(=false) greater than a > b integer or floating point 1(=true) if a is greater than b otherwise 0(=false) greater than or equal to a >= b integer or floating point 1(=true) if a is greater than or equal to b otherwise 0(=false)

example output: (10 > 11) = 0 (10 >= 11) = 0 (10 == 11) = 0 (10 != 11) = 1 (10 < 11) = 1 (10 <= 11) = 1

Logical Operators op nameexpressionmeaing logical NOT ! a If a is false, then 1(=true), otherwise 0(=false) logical AND a && b If both a and b are true, then 1(=true), otherwise 0(=false) logical OR a || b If either a or b is true, then 1(=true), otherwise 0(=false)

example #include int main() { int score; printf("Score?"); scanf("%d",&score); if (score >= 90 && score <=100) printf("your grade is A.\n"); if (score >= 80 && score < 90) printf("your grade is B.\n"); if (score >= 70 && score < 80) printf("your grade is C.\n"); if (score >=60 && score < 70) printf("your grade is D.\n"); if (score < 60) printf("your grade is F.\n"); return 0; }

Automatic Type Conversion What happens when expression has mixture of different data types. Ex) double x=1.2; float y=0.0; int i=3; int j=0; j=x+i; /* (temporary copy of)i will be converted to double type before ‘+’ operation. the value of i in memory is unchanged */ y=x+i; printf(“j=%d, y=%f\n”,j,y);

Automatic Type Conversion “lower” types are promoted to “higher” types. The expression itself will have the type of its highest operand. The type hierarchy is as follows long double double float int short, char If either operand is long double, convert the other to long double Otherwise, if either operand is double, convert the other to double Otherwise, if either operand is float, convert the other to float Otherwise, convert char and short to int

Automatic Type Conversion with assignment operator Example double x=5.5; int y=3; y=x; /* x will be converted to int type */ x=y; /* y will be converted to double type */

Type casting Programmers can enforce type conversion to a variable Ex1) double x=3.5; double y=2.7; double below_point; below_point = x*y - (int)(x*y) ; Ex2) double x=3.5; printf(“integer number of x = %d\n”,(int)x);