[prev] 37 [next]

Assignments

  • In C, each statement is terminated by a semicolon ;
  • Curly brackets { } used to enclose statements in a block
  • Usual arithmetic operators: +, -, *, /, %
  • Usual assignment operators: =, +=, -=, *=, /=, %=
  • The operators ++ and -- can be used to increment a variable (add 1) or decrement a variable (subtract 1)
    • It is recommended to put the increment or decrement operator after the variable:

               // suppose k=6 initially
      k++;     // increment k by 1; afterwards, k=7
      n = k--; // first assign k to n, then decrement k by 1
               // afterwards, k=6 but n=7
      

    • It is also possible (but NOT recommended) to put the operator before the variable:

               // again, suppose k=6 initially
      ++k;     // increment k by 1; afterwards, k=7
      n = --k; // first decrement k by 1, then assign k to n
               // afterwards, k=6 and n=6