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.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
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.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
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,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Review of Lectures 6, 7, 8 and 10. Compilation (1) Compilation translates your source code (in the file hello_world.c) into object code (machine dependent.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
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.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Operators.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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 =.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
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.
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.
Operators And Expressions
Side Effect Operators Changing the value of variables
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
INC 161 , CPE 100 Computer Programming
Variable Declaration, Data types, Expressions
Arithmetic Operator Operation Example + addition x + y
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
All the Operators 22-Nov-18.
More about Numerical Computation
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Chap 7. Advanced Control Statements in Java
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator and Expression
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

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 formed by data and operators An expression in C usually has a value – except for the function call that returns void.

Arithmetic Operators OperatorSymbol Action Example Addition + Adds operandsx + y Subtraction - Subs second from firstx - y Negation - Negates operand-x Multiplication * Multiplies operandsx * y Division / Divides first by secondx / y (integer quotient) Modulus % Remainder of divide opx % y

Assignment Operator x=3 – = is an operator – The value of this expression is 3 – = operator has a side effect -- assign 3 to x The assignment operator = – The side-effect is to assign the value of the right hand side (rhs) to the left hand side (lhs). – The value is the value of the rhs. For example: x = ( y = 3 ) +1; /* y is assigned 3 */ /* the value of (y=3) is 3 */ /* x is assigned 4 */

Compound Assignment Often we use “update” forms of operators – x=x+1, x=x*2,... C offers a short form for this: – Generic Form variable op= expr equivalent to variable = variable op expr – Update forms have value equal to the final value of expr i.e., x=3; y= (x+=3); /* x and y both get value 6 */ OperatorEquivalent to: x *= yx = x * y y -= z + 1y = y - (z + 1) a /= ba = a / b x += y / 8x = x + (y / 8) y %= 3y = y % 3

Increment Operator Other operators with side effects are the pre- and post-increment and decrement operators. – Increment:++++x, x++ ++x is the same as : (x = x + 1) – Has value x old +1 – Has side-effect of incrementing x x++ – Has value x old – Has side-effect of incrementing x – Decrement -- --x, x-- similar to ++

More on Pre/Post Inr/Dcr Op Both forms increment or decrement the appropriate variable, but they do so at different times. ++i (prefix form) increments i before using its value i++ (postfix form) increments it after its value has been used. The postfix increment and decrement operators fetch the current value of the variable and store a copy of it in a temporary location. The compiler then increments or decrements the variable. The temporary copy, which has the variable's value before it was modified, is used in the expression.

Example i=k--; /* Stores the value of k in i then decrements k */ j=l++; /* Stores the value of l in j then increments l */ i=--k; /* Decrements k then stores the new value of k in i */ j=++l; /* Increments l then stores the new value of l in j */

Relational Operators Relational operators allow you to compare variables. – They return a 1 value for true and a 0 for false. OperatorSymbolExample Equals==x == y NOT x = y Greater than>x > y Less than<x < y Greater/equals>=x >= y Less than/equals<=x <= y Not equal !=x != y C uses: – 0 as false – Non-zero integer as true

Logical Operators && AND || OR ! NOT See example program

Operating on Bits C allows you to operate on the bit representations of integer variables. – Generally called bit-wise operators. All integers can be thought of in binary form. – For example, suppose ints have 16-bits = = FFF0 16 = In C, hexadecimal literals begin with 0x, and octal literals begin with 0. x=65520;base 10 x=0xfff0;base 16 (hex) x= ;base 8 (octal)

Operating on Bits Bitwise operators The shift operator: – x << n Shifts the bits in x n positions to the left, shifting in zeros on the right. If x = x << 1 equals – x >> n Shifts the bits in x n positions right. – shifts in the sign if it is a signed integer (arithmetic shift) – shifts in 0 if it is an unsigned integer x >> 1 is (unsigned) x >> 1 is (signed)

Bitwise Logical Operators Work on all integer types & Bitwise AND x=0xFFF0 y=0x002F x&y=0x0020 | Bitwise Inclusive OR x|y=0xFFFF ^ Bitwise Exclusive OR (XOR) x^y=0xFFDF ~ The complement operator ~ y= 0xFFD0 » Complements all of the bits of X

Faster Multiplication and Division Multiplication and division is often slower than shift. Multiplying 2 can be replaced by shifting 1 bit to the left. n = 10 printf(“%d = %d”, n*2, n<<1); printf(“%d = %d”, n*4, n<<2); …… Division by 2 can be replace by shifting 1 bit to the right. n = 10 printf(“%d = %d”, n/2, n>>1); printf(“%d = %d”, n/4, n>>2);

Operator Precedence Operator Precedence level ( ) 1 ~, ++, --, unary - 2 *, /, % 3 +, - 4 > 5, >= 6 ==, != 7 & 8 ^ 9 |10 &&11 ||12 =, +=, -=, etc.14 uWe’ll be adding more to this list later on...

Example What is the difference between the two lines of output? #include int main () { int w=10,x=20,y=30,z=40; int temp1, temp2; temp1 = x * x /++y + z / y; printf ("temp1= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n", temp1, w,x,y,z); y=30; temp2 = x * x /y++ + z / y; printf ("temp2= %d;\nw= %d;\nx= %d;\ny= %d;\nz= %d\n", temp2, w,x,y,z); return 0; }

Conditional Operator The conditional operator essentially allows you to embed an “if” statement into an expression Generic Form exp1 ? exp2 : exp3 if exp1 is true (non-zero) value is exp2 (exp3 is not evaluated) if exp1 is false (0), value is exp3 (exp2 is not evaluated) Example: z = (x > y) ? x : y; This is equivalent to: if (x > y) z = x; else z = y;

Comma Operator An expression can be composed of multiple subexpressions separated by commas. – Subexpressions are evaluated left to right. – The entire expression evaluates to the value of the rightmost subexpression. Example: x = (a++, b++); a is incremented b is assigned to x b is incremented – Parenthesis are required because the comma operator has a lower precedence than the assignment operator! The comma operator is often used in for loops.

Example int i, sum; for (i=0,sum=0;i<100;i++){ sum += i; } printf(“ = %d”, sum);