Back to A Brief Guide to C

note: These are formatted as follows:
  • Operation Name: (operator)
    Optional explanation for really weird operators
    /* Code Sample */



Setting Values

  • Equals, or "gets": =
    x = y;
    x = y = z = 5; /* they can also stack -- all the variables are set to the same value */

Comparing Values

  • Is equal to: ==
    z = y == x; /* z gets '1' (true) if y is equal to x, '0' */ (false) otherwise
  • Is not equal to: !=
    z = y != x; /* z gets '0' (false) if y is equal to x, '1' (true) otherwise */
  • Is greater than: >
    z = y > x; /* z gets '1' (true) if y is greater than x, '0' (false) otherwise */
  • Is greater than or equal to: >=
    z = y >= x; /* z gets '1' (true) if y is greater than or equal to x, '0' (false) otherwise */
  • Is less than: <
    z = y < x;
  • Is less than or equal to: <=
    z = y <= x;
  • Logical And: &&
    z = y && x; /* z gets '1' if both y and x are not 0, '0' otherwise */
  • Logical Or: ||
    z = y || x; /* z gets '1' if y or x are not 0, '0' otherwise */
  • Logical Not: !
    This is useful for setting and comparing values.
    z = !x; /* z gets '1' if x is '0', '0' otherwise */

Arithmetic


  • Addition: +
    z = y + x; /* z gets the value of y plus x */

  • Subtraction: -
    z = y - x; /* z gets the value of y minus x */

  • Multiplication: *
    z = y * x; /* z gets the value of y times x */

  • Addition: /
    z = y / x; /* z gets the value of y divided by x */

  • Modulus: %
    z = y % x; /* z gets the value of y modulo x */


Bit Twiddling


  • Bitwise And: &
    This compares the bits in the two operands; for each bit, it and's the two bits. For instance, 12 (00001100 in binary) & 10 (00001010) = 8 (00001000). The fifth bit -- 8 -- was the only one which appeared in both operands, so it's the only one returned by the operator.
    z = y & x;

  • Bitwise Inclusive Or: |
    This compares the bits in the two operands; for each bit, it or's the two bits. For instance, 12 (00001100 in binary) & 10 (00001010) = 14 (00001110). The fifth, sixth, and seventh bit appeared in the first or second operand, so they were returned by the operator.
    z = y | x;

  • Bitwise Exclusive Or (aka Bitwise XOR): ^
    This compares the bits in the two operands; for each bit, it exclusive or's the two bits. For instance, 12 (00001100 in binary) & 10 (00001010) = 6 (00000110). The sixth and seventh bit appeared in either the first or second operand, so they were returned by the operator, while the fifth bit appeared in both, so a '0' was assigned in that place.
    z = y ^ x;

  • Right / Left Shift: >> <<
    This shifts the bits a specific number of places right or left. Any bits that are shifted off the edge of the variable's memory space are lost. For instance, 7 (00000111) >> 2 = 1 (00000001) << 2 = 4 (00000100).
    z = y >> x;
    z = y << w;

  • Bitiwise (One's) Complement: ~
    This does a one's complement on the operand, that is, it reverses all the bits. So, ~7 (00000111) = 248 (11111000)
    z = ~y

Operator-Equal Sign Operators: Don't Type 5 Characters Where 3 Will Do

  • Addition: +=
    z += x; /* z gets z + x */
  • Subtraction: -=
    z -= x; /* z gets z - x */
  • Multiplication: *=
    z *= x; /* z gets z * x */
  • Division: /=
    z /= x; /* z gets z / x */
  • Modulus: %=
    z %= x; /* z gets z % x */
  • Bitwise And: &=
    z &= x; /* z gets z & x */
  • Bitwise Or: |=
    z |= x; /* z gets z | x */
  • Bitwise XOr: ^=
    z ^= x; /* z gets z ^ x */
  • Right / Left Shift: >>= <<=
    z >>= x;
    z <<= w;

Increment/Decrement: Don't Type 3 Characters Where 2 Will Do

  • Pre/Post Increment: ++
    This operator does different operations depending on which side of the variable it is placed on. If it is placed before the variable, it increments (adds 1 to) the variable before any other operation in that statement. If it is placed after the variable, it increments the variable after everything else in that statement is finished. You may replace this operation with a += 1, placed before or after the statement depending on the operator's position with respect to the variable it modifies.
    z = x++; /* z gets x, then x is incremented by one */
    z = ++x; /* x is incremented, then z gets x */
  • Pre/Post Decrement: --
    This behaves exactly like the ++ operator, except it decrements (decreases by one) its operand.
    z = x--; /* z gets x, then x is decremented */
    z = --x; /* x is decremented, then z gets x */

Other Operators

  • Conditional: ?:
    Generally a bad thing to use, especially when you try to nest them. Try to limit these to really simple decisions.
    z == x ? z++ : x++; /* if z is equal to x, increment z; otherwise, increment x */

  • Sizeof: sizeof()
    Returns the size, in bytes, of the operand. Useful for making your programs non-machine specific (since different processors may have different sized integers, for example).
    z = sizeof(y);

  • Cast: (type)
    To cast a variable into a different type. For instance you might cast an int as a float when doing arithmetic with floats, or cast a float as an int when assigning its value to an int in order to suppress compiler warnings.
    int_z = (int)(float_y / (float) int_w);

Parentheses Change Precidence


  • Parentheses: ()
    Please. Use parentheses rather than depend on the order of precedence. It makes your programs far more readable, and obfuscation is not your friend.
    z = (y + x) % w; /* z gets the value of the sum of y and x, modulo'd by w */

Operator Precedence

Highest precedence, which means it gets done first, is on top; lowest precedence is on the bottom. Operators with the same level of precedence get done left to right, except for the operator-equal sign (like +=) operators, which get done right to left. And none of this really matters since you are going to use parentheses rather than depend on precedence, right?

  • ( ) -> .
  • ++ -- ! ~ * & (type) sizeof()
  • * / %
  • + -
  • << >>
  • < <= > >=
  • == !=
  • &
  • ^
  • |
  • &&
  • ||
  • ?:
  • = += -= *= /= %= &= ^= |= <<= >>=
  • ,

Please /msg me with any errors, inconsistencies, or unclear areas.

Log in or registerto write something here or to contact authors.