C Operators (Chap 2) PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document explains different operators in C programming language. It covers arithmetic, relational, logical, bitwise and assignment operators.
Full Transcript
Chap 2. Operators in C An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. By definition, an operator performs a certain operation on operands. An operator needs one or more operands for the operation to be performed. Depending on how many...
Chap 2. Operators in C An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. By definition, an operator performs a certain operation on operands. An operator needs one or more operands for the operation to be performed. Depending on how many operands are required to perform the operation, operands are called as unary, binary or ternary operators. They need one, two or three operands respectively. Unary operators − ++ (increment), -- (decrement), ! (NOT), ~ (compliment), & (address of), * (dereference) Binary operators − arithmetic, logical and relational operators except ! Ternary operators − The ? Operator C language is rich in built-in operators and provides the following types of operators − Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators Arithmetic Operators- We are most familiar with the arithmetic operators. These operators are used to perform arithmetic operations on operands. The most common arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In addition, the modulo (%) is an important arithmetic operator that computes the remainder of a division operation. Arithmetic operators are used in forming an arithmetic expression. These operators are binary in nature in the sense they need two operands, and they operate on numeric operands, which may be numeric literals, variables or expressions. For example, take a look at this simple expression − a+b Here "+" is an arithmetic operator. We shall learn more about arithmetic operators in C in a subsequent chapter. The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20 then − Operato Description Example r + Adds two operands. A + B = 30 − Subtracts second operand from the first. A − B = -10 * Multiplies both operands. A * B = 200 / Divides numerator by de-numerator. B/A=2 Modulus Operator and remainder of after an integer % B%A=0 division. ++ Increment operator increases the integer value by one. A++ = 11 Decrement operator decreases the integer value by -- A-- = 9 one. Relational Operators- We are also acquainted with relational operators while learning secondary mathematics. These operators are used to compare two operands and return a boolean value (true or false). They are used in a boolean expression. The most common relational operators are less than (), less than or equal to (=), equal to (==), and not equal to (!=). Relational operators are also binary operators, needing two numeric operands. For example, in the Boolean expression − a>b Here, ">" is a relational operator. Operat Description Example or Checks if the values of two operands are equal or not. == (A == B) is not true. If yes, then the condition becomes true. Checks if the values of two operands are equal or not. != If the values are not equal, then the condition (A != B) is true. becomes true. Checks if the value of left operand is greater than the > value of right operand. If yes, then the condition (A > B) is not true. becomes true. Checks if the value of left operand is less than the < value of right operand. If yes, then the condition (A < B) is true. becomes true. Checks if the value of left operand is greater than or >= equal to the value of right operand. If yes, then the (A >= B) is not true. condition becomes true. Checks if the value of left operand is less than or equal = 50 The most common logical operators are AND (&&), OR(||), and NOT (!). Logical operators are also binary operators. Operat Description Example or Called Logical AND operator. If both the operands && (A && B) is false. are non-zero, then the condition becomes true. Called Logical OR Operator. If any of the two || operands is non-zero, then the condition (A || B) is true. becomes true. Called Logical NOT Operator. It is used to reverse ! the logical state of its operand. If a condition is !(A && B) is true. true, then Logical NOT operator will make it false. Bitwise Operators- Bitwise operators let you manipulate data stored in computer’s memory. These operators are used to perform bit-level operations on operands. The most common bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left shift (). Here the "~" operator is a unary operator, while most of the other bitwise operators are binary in nature. Bitwise operator works on bits and perform bit−by−bit operation. The truth tables for &, "|", and "^" are as follows − p q p&q p|q p^q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1 Assume A = 60 and B = 13 in binary format, they will be as follows − A = 0011 1100 B = 0000 1101 ------------------------ A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011 The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then − Operator Description Example Binary AND Operator copies a bit to the result if it exists in both & (A & B) = 12, i.e., 0000 1100 operands. | Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101 Binary XOR Operator copies the bit if it is set in one operand ^ (A ^ B) = 49, i.e., 0011 0001 but not both. Binary One's Complement Operator is unary and has the effect ~ (~A ) = ~(60), i.e,. -0111101 of 'flipping' bits. Binary Left Shift Operator. The left operands value is moved left A >> 2 = 15 i.e., 0000 1111 right by the number of bits specified by the right operand. Assignment Operators- As the name suggests, an assignment operator "assigns" or sets a value to a named variable in C. These operators are used to assign values to variables. The "=" symbol is defined as assignment operator in C, however it is not to be confused with its usage in mathematics. The following table lists the assignment operators supported by the C language − Hence, the expression "a = 5" assigns 5 to the variable "a", but "5 = a" is an invalid expression in C. The "=" operator, combined with the other arithmetic, relational and bitwise operators form augmented assignment operators. For example, the += operator is used as add and assign operator. The most common assignment operators are =, +=, -=, *=, /=, %=, &=, |=, and ^=. Misc Operators ↦ sizeof & ternary Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language. Operat Description Example or sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4. &a; returns the actual address of the & Returns the address of a variable. variable. * Pointer to a variable. *a; If Condition is true ? then value X : ?: Conditional Expression. otherwise value Y Operators Precedence in C Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. Category Operator Associativity Postfix () [] ->. ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative */% Left to right Additive +- Left to right Shift > Left to right Relational < >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left = += -= *= /= %=>>=