1 / 18

# Expressions

Expressions. An Expression is a sequence of operands and operators that reduces to a single value. An operato r is a language-specific syntactical token that requires action to be taken. For example, multilply (*) is an operator . Télécharger la présentation ## Expressions

E N D

### Presentation Transcript

1. Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token that requires action to be taken. For example, multilply (*) is an operator . There is no limit to the number of operator and operand sets that you can combine to form an expression. Example: 5 + 3 * 4 The value of above expression is 17. Multiply, divide, and modulus operators have the highest priority among binary operators and have the fourth level in the Precedence Table

2. The modulus ( % ) operator divides the first operand by the second operand and returns the remainder rather than the quotient. For eg. 5 % 2 returns 1 For modulus operator both operands must be integer types. Contd..

3. Following example will general compile error because Modulus operands must be integers. #include <stdio.h> int x; int main () { x = 7.5 % 3; return 0; }

4. The assignment operator = assigns a value to a variable. For example, x= y If y has the value of 6, then the value of the variable y (which is 6 )is assigned to the variable x. The value of the expression x =y is also 6 The left operand must always be a single variable

5. A compound assignment is a shorthand notation for a simple assignment. Compound assignment operators are: *=, /=, %=, +=, and -= For example:For x*=y, equivalent simple expression is: x = x * y Similarly, x /= y is x = x/y x -= y+ 4 is x = x- ( y+ 4) x %= y is x = x % y x *= y+2 is x = x* ( y+ 2).

6. The postfix increment/decrement operates at the second level of the Precedence Table ( see inside front cover) a ++ Here a is an operand while ++ is a postfix operator. a++ has the same effect as a =a+1. Although the result of both expression is the same, there is a major difference.For example: If variable a contains 4 before the expression is evaluated, the value of the expression a ++ is 4 After the evaluating the expression and its side effects, then only a becomes 5.

7. #include <stdio.h> int main (void) /* example of Postfix increment*/ { int a; a = 5; printf("%d\t", a); printf("%d\t", a++); printf("%d\n", a); return 0; } /* Results: 5 5 6 */

8. Prefix increment/decrement are also shorthand notations for adding or subtracting 1 similar to the Postfix operators. The one major difference is that in Prefix operators, the effect takes place before the expression that contains the operator is evaluated. Which one to choose? When you need the values to be the current contents of a variable, use a postfix operator. When you need the values to be the new contents of the variable, use a prefix operator.

9. #include <stdio.h> int main (void) /* example of Prefix increment*/ { int a; a = 5; printf("%d\t", a); printf("%d\t", ++a); printf("%d\n", a); return 0; } /* Results: 5 6 6 */

10. The sizeof operator tell you the size, in bytes, of whatever type is specified. For example: sizeof(int), sizeof ( -746.565), sizeof operator returns different values according to the specific hardware. For example: int a; int b = sizeof(a);// will give 2 bytes in most PCs while 4 bytes for mainframes and 16 bytes for supercomputers.

11. Precedence is used to determine the order in which different operators in a complex expression are evaluated. 2+ 5*4 means 2 + (5 * 4) The value of above expression is 22 because multiplicity has a higher precedence. -a++ means ( - ( a ++) ) ++ is evaluated first because postfix increment has a higher precedence.

12. #include <stdio.h> int main (void) { int a = 2; int b = 3; int c = 4; printf ("a * b + c is: %d\n", a * b + c); printf ("a * (b + c) is: %d\n", a * (b + c)); return 0; } /* a * b + c is: 10 a * (b + c) is: 14 */

13. Associativity can be either from the left or the right. Associativity is used only when the operators have the same level precedence. Example of left-to-right: * / % * 4* 8 / 2 % 4 * 2 means ((((4* 8) / 2) % 4)* 2 ) Similarly for right-to left, a +=b *=c -=5 means (a +=(b *=(c -=5 )))

14. A Side Effect is an action that results from the evaluation of an expression. int x = 3; x = x +2; Here if the original value of x is 3, the value of the expression on the right is 5, the whole expression also has a value of 5 and as a side effect x receives the value 5.

15. Implicit type conversion : C will automatically convert any intermediate type values from one format to another according to the promotion hierarchy . This is done automatically by the compiler For Example : To add integer and float, first integer is converted to float and then after adding, the float will be converted to integer if the value has to be stored in as an integer.

16. Explicit type conversion is when you convert data from one type to another type yourself rather than the compiler. Uses cast expression operator Example program: contd..

17. char aChar = '\0'; int intNum1= 100, int intNum2 = 45; double fltNum1= 100.0 , double fltNum2= 45.0; double fltNum3; fltNum3 = (double)(intNum1 / intNum2); /* result 2.00 */ fltNum3 = (double)intNum1 / intNum2; /* result 2.22 */ aChar = (char)(fltNum1 / fltNum2); /* result 2 */

18. Statement causesan action to be performed. An expression becomes a statement when there is a semilcolon ( ; ) at the end. A compound statement consists of zero or more statements. It is also known as block Main part of a program is an example of a compound statement

More Related