1 / 22

Operators and expression

Operators and expression. C Expression Formats. Precedence Table for C Expressions. Primary Expression. Binary Expression. Binary Expression. # include <stdio . h> int main ( void ) { /* Local Definitions */ int a = 17; int b = 5; /* Statements */

hedy-hoover
Télécharger la présentation

Operators and expression

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operators and expression

  2. C Expression Formats

  3. Precedence Table for C Expressions

  4. Primary Expression Binary Expression

  5. Binary Expression #include <stdio.h> int main (void) { /*Local Definitions */ int a = 17; int b = 5; /* Statements */ printf("%d + %d = %d\n", a, b, a + b); printf("%d - %d = %d\n", a, b, a - b); printf("%d * %d = %d\n", a, b, a * b); printf("%d / %d = %d\n", a, b, a / b); printf("%d %% %d = %d\n", a, b, a % b); printf("Hope you enjoyed the demonstration.\n"); return 0; } /* main */

  6. Assignment Expression Examples of assignment expressions

  7. Expansion of compound expressions Expansion of compound assignment expressions

  8. Compound Assignment #include <stdio.h> int main (void) { /* Local Definitions */ int x; int y; /* Statements */ x = 10; y = 5; printf("x: %2d | y: %2d ", x, y); printf(" | x *= y: %2d ", x *= y); printf(" | x is now: %2d\n", x); x = 10; printf("x: %2d | y: %2d ", x, y); printf(" | x /= y: %2d ", x /= y); printf(" | x is now: %2d\n", x); x = 10; printf("x: %2d | y: %2d ", x, y); printf(" | x %%= y: %2d ", x %= y); printf(" | x is now: %2d\n", x); return 0; } /* main */

  9. Result of postfix a++ Postfix Expression Expansion of postfix expressions

  10. Postfix Increment #include <stdio.h> int main (void) { /* Local Definitions */ int a; /* Statements */ a = 4; printf("value of a : %2d\n", a); printf("value of a++ : %2d\n", a++); printf("new value of a: %2d\n\n", a); return 0; } /* main */

  11. Unary expression Result of prefix ++a Expansion of prefix operator expressions

  12. Prefix Increment #include <stdio.h> int main (void) { /* Local Definitions */ int a; /* Statements */ a = 4; printf("value of a : %2d\n", a); printf("value of ++a : %2d\n", ++a); printf("new value of a: %2d\n", a); return 0; } /* main */

  13. Precedence and Associativity • Precedence is used to determine the order in which different operators in a complex expression are evaluated • Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression

  14. Precedence Example of precedence program discussion #include <stdio.h> int main (void) { /* Local Definitions */ int a = 10; int b = 20; int c = 30; /* Statements */ printf ("a * b + c is: %d\n", a * b + c); printf ("a * (b + c) is: %d\n", a * (b + c)); return 0; } /* main */ 2 + 3 * 4 ( 2 + ( 3 * 4 ) ) -b++ ( -(b++) )

  15. Association a += b *= c -= 5 (a += (b *= (c -= 5 ))) (a = a + (b = b* (c = c – 5))) If a = 3, b = 5, c = 8 (a = 3 + (b = (5 * (c = 8 – 5))) 3 * 8 / 4 % 4 * 5 ((((3 * 8) / 4) % 4) * 5) Left Associativity Right Associativity

  16. Evaluating Expression (1) a * 4 + b / 2 – c * b Value of variable: a=3 b=4 c=5 1 Replace the variable with values 3 * 4 + 4 / 2 – 5 * 4 2 Evaluate precedence operators * , / binary expression Pr 13 ( 3 * 4 ) + ( 4 / 2 ) – ( 5 * 4 )  12 + 2 – 20 3 Evaluate precedece operators (repeat step 2) - + binary expression Pr 12  -6

  17. Evaluating Expression (2) --a * (3+b) / 2 – c++ * b Value of variable: a=3 b=4 c=5 • 1 Rewrite • Copy prefix increment or decrement and place them before expression. • Replace each removed expression with the variable. • (b) Copy postfix increment or decrement and place them after expression. • Replace each removed expression with the variable. • --a • a * (3+b) / 2 – c * b • c++  a=2 b=4 c=5 2. Replace variable with value & modify expression 2 * (3+4) / 2 – 5 * 4 c++ • 3. Evaluate parentheses 2 * 7 / 2 – 5 * 4 • c++ • 4. Evaluate precedence (repeat)  7 – 20  -13 • c++ • 5. Evaluate posteffect expression  a=2 b=4 c=6

  18. Evaluating expression program #include <stdio.h> int main (void) { /* Local Definitions */ int a = 3; int b = 4; int c = 5; int x; int y; /* Statements */ printf("Initial values of the variables: \n"); printf("a = %d\tb = %d\tc = %d\n\n", a, b, c); x = a * 4 + b / 2 - c * b; printf("Value of a * 4 + b / 2 - c * b: %d\n", x); y = --a * (3 + b) / 2 - c++ * b; printf("Value of --a * (3 + b) / 2 - c++ * b: %d\n", y); printf("\nValues of the variables are now: \n"); printf("a = %d\tb = %d\tc = %d\n\n", a, b, c); return 0; } /* main */

  19. Mixed Type Expression Promotion hierarchy

  20. Implicit Type Conversion #include <stdio.h> int main (void) { /* Local Definitions */ char aChar = 'A'; int intNum = 200; double fltNum = 245.3; /* Statements */ printf("aChar contains : %c\n", aChar); printf("aChar numeric : %d\n", aChar); printf("intNum contains: %d\n", intNum); printf("fltNum contains: %f\n", fltNum); intNum = intNum + aChar; /* aChar promoted to int */ fltNum = fltNum + aChar; /* aChar promoted to float */ printf("\nAfter additions...\n"); printf("aChar numeric : %d\n", aChar); printf("intNum contains: %d\n", intNum); printf("fltNum contains: %f\n", fltNum); return 0; } /* main */

  21. #include <stdio.h> int main (void) { /* Local Definitions */ char aChar = '\0'; int intNum1 = 100; int intNum2 = 45; double fltNum1 = 100.0; double fltNum2 = 45.0; double fltNum3; /* Statements */ printf("aChar numeric : %3d\n", aChar); printf("intNum1 contains: %3d\n", intNum1); printf("intNum2 contains: %3d\n", intNum2); printf("fltNum1 contains: %6.2f\n", fltNum1); printf("fltNum2 contains: %6.2f\n", fltNum2); fltNum3 = (double)(intNum1 / intNum2); printf ("\n(double)(intNum1 / intNum2): %6.2f\n", fltNum3); fltNum3 = (double)intNum1 / intNum2; printf("(double) intNum1 / intNum2 : %6.2f\n", fltNum3); aChar = (char)(fltNum1 / fltNum2); printf(" (char)(fltNum1 / fltNum2): %3d\n", aChar); return 0; } /* main */ Explicit Casts

  22. Statements Expression statement a=2; a = b = 3; a = (b=3); Compound statement

More Related