Sunteți pe pagina 1din 8

1.

Unary Minus Expression ("-") The unary minus operator tells the machine to take the negative of a single value. 2. Casts ("(<type name>)") Technically a cast is an unary operator. 3. Addition ("+") This operator causes the machine to add the two operands together. 4. Subtraction ("-") This operator causes the machine to subtract the second operand from the first. 5. Multiplication ("*") This operator causes the machine to multiply the two operands together and return the result. 6. Real Division ("/") This operator causes the machine to divide the second operand into the first and return a real number. Since this operator looks exactly like the operator for integer division, the compiler decides what to do by looking at the types of the operands - if either operand is a float or a double, the machine will do a real division. If the second operand is 0, you are going to have a problem when this is executed. 7. Integer Division ("/")

By Shilpa Umdekar

This operator causes the machine to divide the second operand into the first and returns the integer quotient. It does this if both operands are positive by truncating the result. If one or both are negative, then the result is implementation dependent. The compiler decides to do an integer division if both operands are integers of some kind. If the second operand is 0, you are going to have a problem when this is executed. 8. Modulus ("%") This operator causes the machine to divide the second operand into the first and returns the remainder. Both operands must be integers of some kind. If the second operand is 0, you are going to have a problem when this is executed. 2. Normal Relational Expressions 1. Equality ("==") This operator causes the machine to compare the two operands to see if they are equal. You have to be careful when comparing real numbers with the equality test. Often slight precision errors that happen because there is only a finite representation for these numbers cause an equality test to fail when it shouldn't. 2. Inequality ("!=") This operator causes the machine to compare the two operands to see if they are not equal.

By Shilpa Umdekar

You have to be careful when comparing real numbers with the inequality test. Often slight precision errors that happen because there is only a finite representation for these numbers cause an inequality test to succeed when it shouldn't. 3. Less Than ("<") This operator causes the machine to compare the two operands to see if the first is less than the second. 4. Less Than Or Equal ("<=") This operator causes the machine to compare the two operands to see if the first is less than or equal to the second. 5. Greater Than (">") This operator causes the machine to compare the two operands to see if the first is greater than the second. 6. Greater Than Or Equal (">=") This operator causes the machine to compare the two operands to see if the first is greater than or equal to the second. 3. Please note, you can only use these relational operators to compare two quantities. In mathematical notation we might write:
4. 5 < 3 < 7

5. to say that "5" is less than "3" which is less than "7". The answer to this is FALSE. But if we do this in C, the compiler evaluates the first one ("5 < 3") to FALSE (which is 0), replaces the first expression with result of evaluating it ("0")

By Shilpa Umdekar

and then evaluates "0 < 7" and comes up with TRUE. If you want to write the above in C, write:
6. (5 < 3) && (3 < 7)

7. Normal Logical Expressions 1. Unary Logical Negation ("!") This operator takes a boolean value and returns its boolean opposite. that is, it return a TRUE (1) if it has a FALSE (0) operand and it returns a FALSE (0) if it has a TRUE (not zero) operand. 2. AND ("&&") This operator causes the machine to take the boolean AND of its operands. Boolean AND is defined as TRUE if both its operands are TRUE, otherwise as FALSE. That means that if the first operand is FALSE, there is no need to evaluate the second operand. So the C compiler doesn't. 3. OR ("||") This operator causes the machine to take the boolean OR of its operands. Boolean OR is defined as TRUE if either of its operands are TRUE, otherwise as FALSE. That means that if the first operand is TRUE, there is no need to evaluate the second operand. 8. Assignment Expressions - Expressions With Side Effects 1. Simple Assignment Expression The simple assignment expression evaluates the expression on the right hand side of the assignment operator and stuff that value into the memory location specified on the left hand side of the assignment operator. To do this, the left hand side has to be a memory location. For example, the following is illegal:
By Shilpa Umdekar

Count + Number = 8

CodeWarrior complains about this by saying that the left hand side is not an "l-value". The term "l-value" essentially means an expression that gives us a memory location. In addition, you can't put a type that has been declared const on the left hand side. 2. Compound Assignment Expressions C also provides us with a number of special assignment expressions. All of these are of the form:
LHS <op>= RHS

They are all equivalent to:


LHS = LHS <op> RHS

Here are the various compound assignment expressions:


+= -= *= /= %=

9. Increment And Decrement Expressions - Expressions With Side Effects C gives us two versions of an expression that will change a memory locations value up or down by one (increment and decrement). They come in two forms - prefix and postfix. Here are some examples:
Count++ ==> postfix --Count ==> prefix

We can use them all by themselves, as in:

By Shilpa Umdekar

++Count;

Used this way, there is no difference between the prefix and the postfix version. They both are the same as:
Count = Count + 1 Count = Count - 1

However, if they are embedded in other expressions, such as:


Number = Number + Count++;

They are different: 1. Prefix Increment And Decrement Expressions Three things happen with the increment and decrement expressions. The operand is evaluated, the operand is incremented or decremented, and a result is produced. The order of the last two things is different between prefix and postfix. In the prefix increment and decrement expressions, the operand is evaluated, then the operand is incremented or decremented, and finally the new value is used as the result. The value that is used as the result is the value of the operand after the increment or decrement. The operand must be an l-value (evaluate to a memory location). 2. Postfix Increment And Decrement Expressions In the postfix increment and decrement expressions, the operand is evaluated, then that value is put aside as the result, finally the operand is incremented or decremented. The value that is used as the result is the value of the operand before the increment or decrement.
By Shilpa Umdekar

The operand must be an l-value (evaluate to a memory location). Here is an example of the difference between the two kinds of increment and decrement expressions. Suppose the value of Count before these expressions is "5":
Number = ++Count; ==> Number is "6" now Number = Count++; ==> Number is "5" now

In both cases, the value of Count is now "6". 10. Precedence And Parentheses

Things get more complicated when you combine lots of different operators in one expression. Here is an example:
Value + Increment * Rate

Here, most of us expect that the multiplication will be done first because that is the way it is in mathematics. And C does just that. We say that the multiplication operator has "higher precedence" (is done first). What about this:
Value + PartA + PartB

In C, these are done from left to right. That is, in C if two operators next to each other have equal precedence, they are left associative (there is an exception to this, but we won't worry about it for now). So now, all we need is the precedence of the various C operators and we can write any combination of expressions. Here they are from highest (done first) to lowest (done last): ()

By Shilpa Umdekar

++, -- (postfix) ++, -- (prefix) ! - (unary) (<type name>) *, /, % +, <, >, <=, >= ==, != && || =, +=, -=, *=, /=, %= The first of these is parenthesis - that is, we can change the order of evaluation by grouping parts of expressions with parenthesis. 11. Other Expressions

This is not the complete list of expressions in C. The others we will either introduce as we go along or we don't consider them critical for this course. You can thumb through the book if you are interested in the rest.

By Shilpa Umdekar

S-ar putea să vă placă și