CHAPTER 4 ========= Operators Associativity ------------------------------------------------------------- () ++(postfix) --(postfix) left to right ------------------------------------------------------------- +(unary) -(unary) ++(prefix) --(prefix) ! right to left ------------------------------------------------------------- * / % left to right ------------------------------------------------------------- + - left to right ------------------------------------------------------------- < <= > >= left to right ------------------------------------------------------------- == != left to right ------------------------------------------------------------- && left to right ------------------------------------------------------------- || left to right ------------------------------------------------------------- ?: right to left ------------------------------------------------------------- = += -= *= /= etc right to left ------------------------------------------------------------- ,(comma operator) left to right %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Relational operators: -------------------------------------------------- Declarations and initializations -------------------------------------------------- char c = `w'; int i = 1, j = 2, k = -7; double x = 7e+33, y = 0.001; -------------------------------------------------- Expression Equivalent expression Value -------------------------------------------------- `a' + 1 < c (`a' + 1) < c 1 -i-5*j >= k+1 ((-i)-(5*j)) >= (k+1) 0 3 < j < 5 (3 int main(void) { int blank_cnt = 0, c, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while ((c = getchar()) != EOF) /* braces not necessary */ if (c == ' ') ++blank_cnt; else if (c >= '0' && c <= '9') ++digit_cnt; else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') ++letter_cnt; else if (c == '\n') ++nl_cnt; else ++other_cnt; printf("%10s%10s%10s%10s%10s%10s\n\n", "blanks", "digits", "letters", "lines", "others", "total"); printf("%10d%10d%10d%10d%10d%10d\n\n", blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt, blank_cnt + digit_cnt + letter_cnt + nl_cnt + other_cnt); return 0; } -------------------------------------------------------------------- blanks digits letters lines others total 208 32 361 27 180 808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% For loops: ---------- i = 1; sum = 0; for ( ; i <= 10; ++i) sum += i; ---------------------------- i = 1; sum = 0; for ( ; i <= 10; ) sum += i++; ---------------------------- i = 1; sum = 0; for ( ; ; ) { sum += i++; printf("%d\n", sum); } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The comma operator: ------------------- for (sum = 0, i = 1; i <=n; ++i) sum += i; for (sum = 0, i = 1; i <= n; sum += i, ++i) ; but not as for (sum = 0, i = 1; i <= n; ++i, sum += i) ; -------------------------------------------------- Declarations and initializations -------------------------------------------------- int i, j, k = 3; double x = 3.3; -------------------------------------------------- Expression Equivalent expression Value -------------------------------------------------- i=1, j=2, ++k+1 ((i=1),(j=2)),((++k)+1) 5 k!=1,++x*2.0+1 (k!=1),(((++x)*2.0)+1) 9.6 -------------------------------------------------- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% do-while: --------- do { printf("Input a positive integer: "); scanf("%d", &n); if (error = (n <= 0)) printf("\nERROR; Do it again!\n\n"); } while (error); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Break & continue: ----------------- while (1) { scanf("%1f", &x); if (x < 0.0) break; /* exit loop if x is negative */ printf("%f\n", sqrt(x)); } /* break jumps to here */ for (i = 0; i < TOTAL; ++i) { c = getchar(); if (c >= `0' && c <= `9') continue; ..... /* process other characters */ /* continue transfers control to here to begin next iteration */ } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Switch: ------- switch (c) { case `a': ++a_cnt; break; case `b': ++b_cnt; break; case `c': case `C': ++cC_cnt; break; default: ++other_cnt; } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The conditional operator: ------------------------------------------------------- Declarations and initializations ------------------------------------------------------- char a = `a', b = `b'; /* a has decimal value 97 */ int i = 1, j = 2; double x = 7.07; ------------------------------------------------------- Expression Equivalent expression Value Type ------------------------------------------------------- i==j?a-1:b+1 (i==j)?(a-1):(b+1) 99 int j%3==0?i+4:x ((j%3)==0)?(i+4):x 7.07 double j%3?i+4:x (j%3)?(i+4):x 5.0 double -------------------------------------------------------