4_Operators.pdf
Document Details
Uploaded by DoctorVisionaryAngel
Seneca Polytechnic
Full Transcript
PRG 155 – Programming Fundamentals Using C 4. C Language Elements Expressions An expression is a combination of variables, constants, and operators Expressions are written according to the syntax of C language In a statement: variable = expression; the expression is...
PRG 155 – Programming Fundamentals Using C 4. C Language Elements Expressions An expression is a combination of variables, constants, and operators Expressions are written according to the syntax of C language In a statement: variable = expression; the expression is evaluated first and then the previous value of the variable on the left is replaced. The =, +=, -=, *=, /=, and %= operators are always applied last in an expression. Examples: num1 = num2 a + b x = y + z price 3) > right operand. If yes, then the condition becomes TRUE and 1 is returned. Otherwise, 0 is returned (FALSE). returns 1 Checks if the value of left operand is less than the value of right (8 < 3) < operand. If yes, then the condition becomes TRUE and 1 is returned. Otherwise, 0 is returned (FALSE). returns 0 Checks if the value of left operand is greater than or equal to the (8 >= 3) >= value of right operand. If yes, then the condition becomes TRUE and 1 is returned. Otherwise, 0 is returned (FALSE). returns 1 Checks if the value of left operand is less than or equal to the (8 = 20 && number2 == 20 !(number1 < 5 && number2 > number1) 5 PRG 155 – Programming Fundamentals Using C Operator Precedence Operator Description Priority () Parentheses (function call) Highest [] Brackets (array subscript). Member selection via object name -> Member selection via pointer Postfix increment/decrement (see Note 2) ++ – ++ – Prefix increment/decrement +– Unary plus/minus !~ Logical negation/bitwise complement (type) Cast (convert value to temporary value of type) * Dereference & Address (of operand) sizeof Determine size in bytes on this implementation * / % Multiplication/division/modulus + – Addition/subtraction > Bitwise shift left, Bitwise shift right < >= Relational greater than/greater than or equal to == != Relational is equal to/is not equal to & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR && Logical AND || Logical OR ?: Ternary conditional = Assignment += -= Addition/subtraction assignment *= /= Multiplication/division assignment %= &= Modulus/bitwise AND assignment ^= |= Bitwise exclusive/inclusive OR assignment = Bitwise shift left/right assignment 6 PRG 155 – Programming Fundamentals Using C , Comma (separate expressions) Lowest References Tan, H.H., and T.B. D’Orazio. C Programming for Engineering & Computer Science. USA: WCB McGRaw-Hill. 1999. Print. Tutorialspoint.com. "C Operators." Www.tutorialspoint.com. N.p., n.d. Web. 01 Feb. 2017.. "C Programming Operators." C Operators: Arithmetic, Logical, Conditional and more. N.p., n.d. Web. 01 Feb. 2017.. Rajinikanth. "C Programming Language." C Operators | c operators | operators in c | C by Rajinikanth | C Programming Language. N.p., n.d. Web. 09 Feb. 2017.. 7