Your SlideShare is downloading. ×
0
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
C Prog. - Operators and Expressions
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C Prog. - Operators and Expressions

4,562

Published on

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,562
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
336
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. C Programming - Operators and Expressions <ul><li>2 </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 2. Programming in C: A Practical Approach Operators and Expressions <ul><li>Expressions </li></ul><ul><li>An expression in C is made up of one or more operands. </li></ul><ul><li>The simplest form of an expression consists of a single operand. For example, 3 is an expression that consists of a single operand i.e. 3. Such an expression does not specify any operation to be performed and is not meaningful. </li></ul><ul><li>For example, </li></ul><ul><li>a=2+3 // meaningful expression </li></ul><ul><li>Involves three operands :a, 2 and 3 </li></ul><ul><li>Two operators i.e. = (assignment operator) and + (arithmetic addition operator). </li></ul><ul><li>Thus, an expression is a sequence of operands and operators that specifies the computation of a value. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 3. Programming in C: A Practical Approach Operators and Expressions <ul><li>Operands </li></ul><ul><li>An operand specifies an entity on which an operation is to be performed. </li></ul><ul><li>An operand can be a variable name, a constant. </li></ul><ul><li>For example, a=b+2 is a valid expression involving three operands namely, </li></ul><ul><li>a variable name i.e. a, </li></ul><ul><li>a variable name i.e. b, </li></ul><ul><li>a constant i.e. 2. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 4. Programming in C: A Practical Approach Operators and Expressions <ul><li>Operators </li></ul><ul><li>An operator specifies the operation to be applied to its operands. </li></ul><ul><li>For example, the expression: </li></ul><ul><li>a=b+2; involves two operators namely, </li></ul><ul><li>arithmetic addition operator i.e. + </li></ul><ul><li>assignment operator i.e. =.   </li></ul><ul><li>Based on the number of operators present in an expression, expressions are classified as simple expressions and compound expressions. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 5. <ul><li>Simple and Compound Expressions </li></ul>Programming in C: A Practical Approach Operators and Expressions <ul><li>Simple Expressions </li></ul><ul><li>An expression that has only one operator is known as simple expression . </li></ul><ul><li>E.g., a+2 is a simple expression. </li></ul><ul><li>Evaluation of simple expression is easier as order of determination is trivial in this case as there is only one operator. </li></ul><ul><li>Compound Expressions </li></ul><ul><li>An expression that involves more than one operator is called compound expression. </li></ul><ul><li>E.g. b=2+3*5 is a compound expression. </li></ul><ul><li>Whereas the evaluation of a compound expression requires the correct order in which the operators will operate. This depends upon the Precedence and the Associativity of operators. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 6. <ul><li>Precedence of Operators </li></ul><ul><li>Each operator in C has a precedence associated with it. </li></ul><ul><li>In a compound expression, if the operators involved are of different precedence, the operator of higher precedence is evaluated first. </li></ul><ul><li>For example, in an expression b=2+3*5, </li></ul><ul><li>The sub-expression 3*5 involving multiplication operator (i.e. *). </li></ul><ul><li>The result of evaluation of an expression is an r-value. The sub-expression 3*5 evaluates to an r-value 15. This r-value will act as second operand for addition operator and the expression becomes b=2+15. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 7. Programming in C: A Practical Approach Operators and Expressions <ul><li>In the resultant expression, the sub-expression 2+15. The expression after the evaluation of the addition operator reduces to b=17. </li></ul><ul><li>Now, there is only one operator in the expression. The assignment operator will operate and the value 17 is assigned to b. </li></ul><ul><li>The precedence order is * &gt; + &gt; =. </li></ul><ul><li>The knowledge of precedence of operators alone is not sufficient to evaluate a compound expression in case two or more operators involved are of same precedence. To determine which of these operators will operate first, the Associativity of these operators is to be considered. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 8. Programming in C: A Practical Approach Operators and Expressions <ul><li>Associativity of Operators </li></ul><ul><li>Key Points </li></ul><ul><li>An operator can be either left-to-right associative or right-to-left associative. </li></ul><ul><li>The operators with same precedence always have the same associativity. </li></ul><ul><li>If operators are left-to-right associative, they are applied in left-to-right order i.e. the operator which appears towards left will be evaluated first. </li></ul><ul><li>If they are right-to-left associative, they will be applied in the right-to-left order. </li></ul><ul><li>The multiplication and the division operators are left-to-right associative. Hence, in expression 2*3/5, the multiplication operator is evaluated prior to the division operator as it appears before the division operator in left-to-right order. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 9. <ul><li>Classification of Operators </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. TERNARY OPERATOR BINARY OPERATOR UNARY OPERATOR BASED ON NO. OF OPERATORS BASED ON ROLE OF OPERATOR BITWISE OPERATORS MISCELLANEOUS OPERATORS ARITHMETIC OPERATORS ASSIGNMENT OPERATORS LOGICAL OPERATORS RELATIONAL OPERATORS CLASSIFICATION OF OPERATORS
  • 10. <ul><li>Based on No. of Operators </li></ul><ul><li>Unary Operators </li></ul><ul><li>Unary operator operates on only one operand. </li></ul><ul><li>For example, in the expression -3, </li></ul><ul><li>- is unary minus operator as it operates on only one operand i.e. 3. The operand can be present towards the right of unary operator, as in -3 or towards the left of unary operator, as in the expression a++. </li></ul><ul><li>Examples of unary operators are: </li></ul><ul><li>&amp; (address-of operator) </li></ul><ul><li>sizeof operator </li></ul><ul><li>! (logical negation) </li></ul><ul><li>~ (bitwise negation) </li></ul><ul><li>++ (increment operator) </li></ul><ul><li>-- (decrement operator) etc. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 11. <ul><li>Binary Operators </li></ul><ul><li>Binary operator operates on two operands. It requires an operand towards its left and right. </li></ul><ul><li>For example, in expression 2-3 </li></ul><ul><li>- is acting as binary minus operator as it is operating on two operands i.e. 2 and 3. </li></ul><ul><li>Examples of binary operators are: </li></ul><ul><ul><ul><ul><li>* (multiplication operator), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/ (division operator), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&lt;&lt; (left shift operator), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>== (equality operator), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&amp;&amp; (logical AND), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>&amp; (bitwise AND) etc. </li></ul></ul></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 12. <ul><li>Ternary Operators </li></ul><ul><li>Ternary operator operates on three operands. </li></ul><ul><li>Conditional operator (i.e. ? :) is the only ternary operator available in C. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 13. <ul><li>Based on Role Of Operator </li></ul><ul><li>Arithmetic Operators </li></ul><ul><li>The arithmetic operations like addition, subtraction, multiplication, division etc. can be performed by using arithmetic operators. Following arithmetic operators are available in C: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. The operators within a row have the same precedence and the order in which they are written does not matter. S.No Operator Name of Operator Category -ary of Operators Precedence amongst arithmetic class Associativity 1. + - ++ -- Unary plus Unary minus Increment Decrement Unary operators Unary Level-I (Highest) R-&gt;L (Right-to-left) 2. * / % Multiplication Division Modulus Multiplicative operators Binary Level-II (Intermediate) L-&gt;R (Left-to-right) 3. + - Addition Subtraction Additive operators Binary Level-III (Lowest) L-&gt;R
  • 14. <ul><li>Key Points: Arithmetic Operators </li></ul><ul><li>The parenthesized sub-expressions are evaluated first. </li></ul><ul><li>If the parentheses are nested, the innermost sub-expression is evaluated first. </li></ul><ul><li>The precedence rules are applied to determine the order of application of operators while evaluating sub-expressions. </li></ul><ul><li>The associativity rule is applied when two or more operators of the same precedence appear in the sub-expression. </li></ul><ul><li>If the operands of a binary arithmetic operator are of different but compatible types, C automatically applies arithmetic type conversion to bring the operands to a common type. This automatic type conversion is known as implicit type conversion . The result </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 15. <ul><li>of evaluation of operator will be of common type . The basic principle behind the implicit arithmetic type conversion is that if operands are of different types, the lower type (i.e. smaller in size) should be converted to a higher type (i.e. bigger in size) so that there is no loss in value or precision. Since, a lower type is converted to a higher type, it is said that lower type is promoted to a higher type and the conversion is known as promotion . </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 16. <ul><li>Arithmetic Type Conversions </li></ul><ul><ul><li>If one operand is long double, the other will be converted to long double and the result will be long double. </li></ul></ul><ul><ul><li>If one operand is double, the other will be converted to double and the result will be double. </li></ul></ul><ul><ul><li>If one operand is float, the other will be converted to float and the result will be float. </li></ul></ul><ul><ul><li>If one of the operands is unsigned long int, the other will be converted to unsigned long int and the result will be unsigned long int. </li></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 17. <ul><ul><li>If one operand is long int and other is unsigned int then </li></ul></ul><ul><ul><ul><li>If unsigned int can be converted to long int, then unsigned int operand will be converted as such and the result will be long int. </li></ul></ul></ul><ul><ul><ul><li>Else, both operands will be converted to unsigned long int and the result will be unsigned long int. </li></ul></ul></ul><ul><ul><li>If one of the operands is long int, the other will be converted to long int and the result will be long int. </li></ul></ul><ul><ul><li>If one operand is unsigned int, the other will be converted to unsigned int and the result will be unsigned int. </li></ul></ul><ul><li>If none of the above is carried out, both the operands are converted to int. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 18. <ul><li>Key Points: Arithmetic Operators </li></ul><ul><li>1. The unary plus operator can appear only towards the left side of its operand. </li></ul><ul><li>2. The unary minus operator can appear only towards the left side of its operand. </li></ul><ul><li>3. Increment operator </li></ul><ul><ul><ul><ul><li>The increment operator can appear towards the left side or towards the right side of its operand. If it appears towards the left side of its operand (e.g. ++a), it is known as pre-increment operator . If it appears towards the right side of its operand (e.g. a++), it is known as post-increment operator . </li></ul></ul></ul></ul><ul><ul><ul><ul><li>The increment operator can only be applied to an operand that has a modifiable L-value. If it is applied to an operand that does not have a modifiable L-value, there will be “L-value required” error. </li></ul></ul></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 19. <ul><ul><ul><ul><li>++a or a++ is equivalent to a=a+1. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>The difference between pre-increment and post-increment lies in the point at which the value of their operand is incremented. </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>In case of pre-increment operator, first the value of its operand is incremented and then it is used for the evaluation of expression. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>In case of post-increment operator, the value of operand is used first for the evaluation of the expression and after its use, the value of the operand is incremented. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Increment operator is a token i.e. one unit. There should be no white space character between two ‘+’ symbols. If white space is placed between two ‘+’ symbols, they become two unary plus (+) operators. </li></ul></ul></ul></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 20. Programming in C: A Practical Approach Operators and Expressions <ul><li>Tokens are the basic building blocks of a source code. Characters are combined into tokens according to the rules of the programming language. There are five classes of tokens: identifiers, reserved words, operators, separators and constants. </li></ul>Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. <ul><li>4. Decrement operator </li></ul><ul><ul><ul><li>The decrement operator can appear towards the left side or towards the right side of its operand. If it appears towards the left side of its operand (e.g. --a), it is known as pre-decrement operator . If it appears towards the right side of its operand (e.g. a--), it is known as post-decrement operator. </li></ul></ul></ul><ul><ul><ul><li>The decrement operator can only be applied to an operand that has a modifiable L-value. If it is applied on an operand that does not have a modifiable L-value, there will be a compilation error “L-value required”. </li></ul></ul></ul>
  • 21. <ul><ul><li>--a or a-- is equivalent to a=a-1. </li></ul></ul><ul><ul><li>The difference between pre-decrement and post-decrement lies in the point at which the value of their operand is decremented. </li></ul></ul><ul><ul><ul><ul><ul><li>In case of pre-decrement operator, firstly the value of its operand is decremented and then used for the evaluation of the expression in which it appears. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>In case of post-decrement operator, firstly the value of operand is used for the evaluation of the expression in which it appears and then, its value is decremented. </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Decrement operator is a token i.e. one unit. There should be no white space character between two ‘-’ symbols. If white space is placed between two ‘-’ symbols, they become two unary minus (-) operators. </li></ul></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 22. <ul><li>5. Division operator </li></ul><ul><ul><li>The division operator is used to find the quotient. </li></ul></ul><ul><li>The sign of the result of evaluation of division operator depends upon the sign of both numerator as well as denominator. If both are positive, the result will be positive. If both are negative, the result will be positive. If either of the two is negative, the result will be negative. For example: 4/3=1, -4/3=-1, 4/-3=-1 and -4/-3=1. </li></ul><ul><li>6. Modulus operator </li></ul><ul><ul><li>The modulus operator is used to find the remainder. </li></ul></ul><ul><li>The operands of modulus operator (i.e. %) must be of integer type. Modulus operator cannot have operands of floating point type. </li></ul><ul><li>The sign of the result of evaluation of modulus operator depends only upon the sign of numerator. If sign of numerator is positive, the sign of result will be positive else negative. For example: 4%3=1, -4%3=-1, 4%-3=1 and -4%-3=-1. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 23. <ul><li>Relational Operators </li></ul><ul><li>Relational operators are used to compare two quantities (i.e. their operands). There are six relational operators in C: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category -ary of Operators Precedence amongst relational class Associativity 1. &lt; &gt; &lt;= &gt;= Less than Greater than Less than or equal to Greater than or equal to Relational operators Binary Level-I L-&gt;R 2. == != Equal to Not equal to Equality operators Binary Level-II L-&gt;R
  • 24. <ul><li>Key Points: Relational Operators </li></ul><ul><li>There should be no white space character between two symbols of a relational operator. </li></ul><ul><li>The result of evaluation of a relational expression (i.e. involving relational operator) is a boolean constant i.e. 0 or 1. </li></ul><ul><li>Each of the relational operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. </li></ul><ul><li>The expression a&lt;b&lt;c is valid and is not interpreted as in ordinary mathematics. Since, less than operator (i.e. &lt;) is left-to-right associative, the expression is interpreted as (a&lt;b)&lt;c. This means that “if a is less than b, compare 1 with c, otherwise, compare 0 with c”. </li></ul><ul><li>An expression that involves a relational operator forms a condition . For example, a&lt;b is a condition. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 25. <ul><li>Logical Operators </li></ul><ul><li>Logical operators are used to logically relate the expressions. The C language has the following logical operators: </li></ul><ul><li> In C language, there is no operator available for logical eXclusive-OR (XOR) operation. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category -ary of Operators Precedence amongst logical class Associativity 1. ! Logical NOT Unary Unary Level-I R-&gt;L 2. &amp;&amp; Logical AND Logical operator Binary Level-II L-&gt;R 3. || Logical OR Logical operator Binary Level-III L-&gt;R
  • 26. <ul><li>Key Points: Logical Operators </li></ul><ul><li>Logical operators consider operand as an entity, a unit. </li></ul><ul><li>Logical operators operate according to the following truth tables: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. OPERAND 1 OPERAND 2 AND OPERATION OR OPERATION FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE OPERAND NOT OPERATION FALSE TRUE TRUE FALSE
  • 27. <ul><li>If an operand of a logical operator is a non-zero value, the operand is considered as true. If operand is zero, it is considered as false. </li></ul><ul><li>Each of the logical operators yields 1 if the specified relation evaluates to true and 0 if it evaluates to false. The evaluation is done according to the truth tables mentioned in previous slide. The result has type int. </li></ul><ul><li>The logical AND (i.e. &amp;&amp;) operator and the logical OR (i.e. ||) operator guarantee left to right evaluation. </li></ul><ul><li>Expressions connected by &amp;&amp; or || are evaluated left to right and the evaluation stops as soon as truthfulness or falsehood of the result is </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 28. <ul><li>known. Thus, in an expression: </li></ul><ul><ul><ul><ul><li>E1&amp;&amp;E2, where E1 and E2 are sub-expressions, E1 is evaluated first. If E1 evaluates to 0 (i.e. false), E2 will not be evaluated and the result of overall expression will be 0 (i.e. false). If E1 evaluates to a non-zero value (i.e. true) then E2 will be evaluated to determine the truth value of overall expression. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>E1||E2, where E1 and E2 are sub-expressions, E1 is evaluated first. If E1 evaluates to a non-zero value (i.e. true), E2 will not be evaluated and the result of overall expression will be 1 (i.e. true). If E1 evaluates to 0 (i.e. false) then E2 will be evaluated to determine the truth value of overall expression. </li></ul></ul></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 29. <ul><li>Bitwise Operators </li></ul><ul><li>The C language provides six operators for bit manipulation. These operators do not consider operand as one entity and operate on the individual bits of operands. The following bitwise operators are available in C: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category -ary of Operators Precedence amongst bitwise class Associativity 1. ~ Bitwise NOT Unary Unary Level-I R-&gt;L 2. &lt;&lt; &gt;&gt; Left Shift Right Shift Shift operators Binary Level-II L-&gt;R 3. &amp; Bitwise AND Bitwise operator Binary Level-III L-&gt;R 4. ^ Bitwise X-OR Bitwise operator Binary Level-IV L-&gt;R 5. | Bitwise OR Bitwise operator Binary Level-V L-&gt;R
  • 30. <ul><li>Assignment Operators </li></ul><ul><li>A variable can be assigned a value by using assignment operator. </li></ul><ul><li>The following assignment operators are available in C language: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S. No Operator Name of Operator Category -ary of Operators Precedence Associativity 1. = *= /= %= += -= &amp;= |= ^= &lt;&lt;= &gt;&gt;= Simple assignment Assign product Assign quotient Assign modulus Assign sum Assign difference Assign bitwise AND Assign bitwise OR Assign bitwise XOR Assign left shift Assign right shift Assignment Shorthand assignment operators Binary Level-I R-&gt;L
  • 31. <ul><li>Key Points: Assignment Operators </li></ul><ul><li>The operand that appears towards the left side of an assignment operator should have a modifiable l-value. If the operand appearing towards the left side of the assignment operator does not have modifiable l-value, there will be a compilation error “L-value required”. </li></ul><ul><li>The shorthand assignment is of the form op1 op=op2, where op1 and op2 are operands and op= is a shorthand assignment operator. It is a shorter way of writing op1 = op1 op op2. For example, a/=2 is equivalent to a=a/2.  </li></ul><ul><li>There should be no white space character between two symbols of short-hand assignment operators. </li></ul><ul><li>If two operands of an assignment operator are of different types, the type of operand on the right side of assignment operator is </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 32. <ul><li>automatically converted to the type of operand present on its left </li></ul><ul><li>side. To carry out this conversion, either promotion or demotion is applied. </li></ul><ul><li>The terms assignment and initialization are related but it is important to note the differences between them: </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Initialization Assignment 1. First time assignment at the time of definition is called initialization. For example: int a=10; is initialization of a . Value of a data object after initialization can be changed by the means of assignment. For example: Consider the following statements int a=10; a=20; The value of a is changed to 20 by the assignment statement. 2. Initialization can be done only once. Assignment can be done any number of times. 3. Qualified constant can be initialized with a value. For example, const int a=10; is valid. Qualified constant cannot be assigned a value. It is erroneous to write a=10; if a is a qualified constant.
  • 33. <ul><li>Miscellaneous Operators </li></ul><ul><li>Other operators available in C are: </li></ul><ul><li>Function call operator (i.e. ()) </li></ul><ul><li>Array subscript operator (i.e. []) </li></ul><ul><li>Member select operator </li></ul><ul><ul><li>Direct member access operator (i.e. . (dot operator or period)) </li></ul></ul><ul><ul><li>Indirect member access operator (i.e. -&gt; (arrow operator)) </li></ul></ul><ul><li>Indirection operator (i.e. *) </li></ul><ul><li>Conditional operator </li></ul><ul><li>Comma operator </li></ul><ul><li>sizeof operator </li></ul><ul><li>Address-of operator (i.e. &amp;) </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 34. <ul><li>Sizeof Operator </li></ul><ul><li>The sizeof operator is used to determine the size in bytes that a value or a data object will take in memory. </li></ul><ul><li>The following are the important points about sizeof operator: </li></ul><ul><li>The general form of sizeof operator is: </li></ul><ul><ul><li>sizeof expression or sizeof (expression) </li></ul></ul><ul><ul><li>(For example: sizeof 2, sizeof(a), sizeof(2+3)) </li></ul></ul><ul><ul><li>sizeof (type-name) (For example: sizeof(int), sizeof(int*), sizeof(char)) </li></ul></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category ary of Operators Precedence Associativity 1. sizeof Size-of operator Unary Unary Level-I R-&gt;L
  • 35. <ul><li>Parentheses are must if sizeof operator is applied on a type-name, as indicated in the point 1 b) above. </li></ul><ul><li>The type of result of evaluation of sizeof operator is int. </li></ul><ul><li>The operand of sizeof operator is not evaluated. </li></ul><ul><li>The sizeof operator cannot be applied on operands of incomplete type or function type. </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 36. <ul><li>Combined Precedence of All Operators </li></ul><ul><li>Till now we have discussed intra-class precedence, now we will discuss the inter-class precedence </li></ul>Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category -ary of Operators Precedence Associativity 1. () [] -&gt; . Function call Array subscript Indirect member access Direct member access Level-I (Highest) 2. ! ~ + - ++ -- &amp; * sizeof Logical NOT Bitwise NOT Unary plus Unary minus Increment Decrement Address-of Deference Sizeof Unary Unary Level-II R-&gt;L 3. * / % Multiplication Division Modulus Multiplicative operators Binary Level-III L-&gt;R
  • 37. Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category ary of Operators Precedence Associativity 4. + - Addition Subtraction Additive operators Binary Level-IV L-&gt;R 5. &lt;&lt; &gt;&gt; Left Shift Right Shift Shift operators Binary Level-V L-&gt;R 6. &lt; &gt; &lt;= &gt;= Less than Greater than Less than or equal to Greater than or equal to Relational operators Binary Level-VI L-&gt;R 7. == != Equal to Not equal to Equality operators Binary Level-VII L-&gt;R 8. &amp; Bitwise AND Bitwise operator Binary Level-VIII L-&gt;R 9. ^ Bitwise X-OR Bitwise operator Binary Level-IX L-&gt;R 10. | Bitwise OR Bitwise operator Binary Level-X L-&gt;R 11. &amp;&amp; Logical AND Logical operator Binary Level-XI L-&gt;R 12. || Logical OR Logical operator Binary Level-XII L-&gt;R 13. ?: Conditional operator Conditional Ternary Level-XIII R-&gt;L
  • 38. Programming in C: A Practical Approach Operators and Expressions Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. S.No Operator Name of Operator Category ary of Operators Precedence Associativity 14. = *= /= %= += -= &amp;= |= ^= &lt;&lt;= &gt;&gt;= Simple assignment Assign product Assign quotient Assign modulus Assign sum Assign difference Assign bitwise AND Assign bitwise OR Assign bitwise XOR Assign left shift Assign right shift Assignment &amp; Shorthand assignment operators Binary Level-XIV R-&gt;L 15. , Comma operator Comma Binary Level-XV (Least) L-&gt;R

×