COSC 1315 Fundamentals of Programming
Operators


C/C++ is rich in operators. In order to grasp them we will break them down into groups of arithmetic, increment and assignment.
Later we will addd relational, logical, bitwise and miscellaneous.
  1. C++/C has the following standard arithmetic operators.
    1. + addition
      Associative and communicative.
      int iNum1, iNum2, iNum3;
      iNum1=7;
      iNum2=iNum1 + 4; 
      iNum3=iNum1 + iNum2 + 18;
      
    2. - subtraction
      iNum1= iNum3 -13;
      
    3. * multiplication
      Associative, communicative and distributive.
      iNum3=iNum2 * 3 + iNum1 * 3;
      
    4. / division.
      Note there is only one division operator for both integer and floating point division.
      int iResult;
      float fResult;
      iResult=7/3;
      fResult=7.0/3.0;
      
    5. % modulus,
      computes the remainder from a division, this is defined for integer data types only.
      iResult=7%3;
      
    6. C does not have an operator for exponentiation.
      So in order to raise a value to a power uou must use the pow function like:
      #include <cmath>
      float x,y,z;
      x=pow(y,z);
      
      This will place yz in x.


  2. C has two operators for incrementing and decrementing. (This usually means adding or subtracting one, except when we get to pointers).
    1. ++ increment
    2. -- decrement

      These operator must refer to a memory location also called an lvalue or lhs (i.e. an address) not an expression.

      These operators have a prefix and postfix notation. In postfix notation the variable is changed after its value has been accessed. In prefix notation the change happens before accessing the value of the variable.
      int iNum1, iNum2;
      iNum1=Num2=2;
      iNum1++:
      ++iNum2++;
      
      iNum1=iNum2++ + 3;
      iNum1=3;
      iNum1=++iNum2 + 3;
      


  3. C++ has the following assignment operators
    1. = assignment,
      This operator is evaluated right to left so. a=b=c=2; is a valid c statement.
    2. C++ also has shorthand assignment operator so that a variable does not need to be specified twice in a expression.
      1. += add and assign a+=b means a=a+b
      2. -=
      3. *=
      4. /=
      5. %=


  4. The order of precedence is summarized in the following table as well as evaluation rules. [1035]
    ClassOperatorMeaningLevelEvaluateExample
    Arithmetic+addition3lra+b
    .-subtraction3lra-b
    .*multiplication2lra*b
    ./division2lra/b
    .%modulus2lra%b
    Increment++a=a+11rla++,++a
    Decrement--a=a-11rla--,--a
    Relational>greater than5lra>b
    .<less than 5lra
    .>=greater than/equal5lra>=b
    .<=less than/equal5lra<=b
    .==equal6lra==b
    .!=not equal6lra!=b
    Logical&&and10lra&&b
    .||or11lra||b
    .!not1rl!a
    Bitwise&and7lra&b
    .|inclusive or9lra|b
    . ^exclusive or8lra^b
    .~negation1rl~a
    .<<shift left4lra<<3
    .>>shift right4lra>>3
    Assignment =simple13rla=b, a=b=c
    .+=a=a+b13rla+=b
    .-=a=a-b13rla-=b
    .*=a=a*b13rla*=b
    ./=a=a/b13rla/=b
    .%=a=a%b13rla%=b
    .>>=a=a>>b13rla>>=3
    .<<=a=a<13rla<<=b
    .&=a=a&b13rla&=b
    .|=a=a|b13rla|=b
    .^=a=a^b13rla^=b
    Conditional? : conditional 12rl a ? b : c
    Pointer*indirect1rl*p
    .&address1rl&p


  5. Exercises: Evaluate each of the following:


    1. x = (2 + 3) * 6;

    2. x = (12 + 6)/2*3;

    3. x = - 3 + 4 * 5 - 6;

    4. x = 3 + 4 % 5 - 6;

    5. int x =2, y, z; x *= 3 + 2;

    6. int x =2, y, z; x *= y = z = 4;

    7. int x=2,y=1,z=0; x = y = 1;

    8. int x=2,y=1,z=0; z = x ++ + ++ y;

    9. int a = 1, b = 2 , c = 3, d = 4;

      a += b += c += d;

    10. int x = 10, y =5;

      x = x++ * --y;