C langugae : OPERATORS

OPERATORS
C is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages. There are four main classes of operators: arithmetic, relational , logical, and bitwise. In addition, there are some special operators, such as the assignment operator, for particular tasks.
Arithmetic Operators
A C Arithmetic operator is a symbol used for adding, subtracting, multiplying, dividing, as well as other operations. C operators are not always mathematical in nature, but many are. Table explains these operator symbols and their primary meanings.

Operator

Meaning

Example

Result

*

Multiplication

x * y

The product of x and y

/

Division

x / y

The quotient of x by y

%

The modulo operation

x % y

The remainder of x divided by y

+

Addition

x + y

The sum of x and y

-

Subtraction

x - y

The difference of x and y

+ (unary)

Positive sign

+x

The value of x

- (unary)

Negative sign

-x

The arithmetic negation of x

The Unary Operators
A unary operator operates on, or affects, a single value. For instance, you can assign a variable a positive or negative number by using a unary + or -. You can also assign a variable another positive or negative number by using a unary + or -. Assignment Operators
You can use the assignment operator within any valid expression. This is not the case with most computer languages (including Pascal, BASIC, and FORTRAN), which treat the assignment operator as a special case statement. The general form of the assignment operator is
variable_name=erpression

Operator

Meaning

Example

Result

=

Simple assignment

x = y

Assign x the value of y.

+=  -=
*=  /=  %=
&=  ^=  |=
<<=  >>=

Compound assignment

x *= y

For each binary arithmetic or binary bitwise operator op, x op= y is equivalent to x = x op (y).

Increment and Decrement Operators
C includes two useful operators that simplify two common operations. These are the increment and decrement operators, ++ and – –. The operator ++ adds 1 to its operand, and – – subtracts 1. Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand.

Operator

Meaning

Side effect

Value of the expression

Postfix:

x++

Prefix:

++x

Increment

Increases the value of x by one (like x = x + 1).

The value of x++ is the value that x had before it was incremented.

The value of ++x is the value that x has after it has been incremented.

Postfix:

x--

Prefix:

--x

Decrement

Decreases the value of x by one (like x = x - 1).

The value of x-- is the value that x had before it was decremented.

The value of --x is the value that x has after it has been decremented.

These operators require a modifiable lvalue as their operand. More specifically, the operand must have a real arithmetic type (not a complex type), or an object pointer type. The expressions ++x and --x are equivalent to (x += 1) and (x -= 1).
Comparative Operators
The comparative operators , also called the relational operators and the equality operators , compare two operands and yield a value of type int. The value is 1 if the specified relation holds, and 0 if it does not.This is possible through the use of relational operators that conditionally control other statements. Relational operators first ''look" at the constants and variables in the program, and then operate according to what they "find." This may sound like difficult programming, but it is actually quite straightforward and intuitive.

Operator

Meaning

Example

Result (1 = true, 0 = false)

<

Less than

x < y

1 if x is less than y, otherwise 0

<=

Less than or equal to

x <= y

1 if x is less than or equal to y, otherwise 0

>

Greater than

x > y

1 if x is greater than y, otherwise 0

>=

Greater than or equal to

x >= y

1 if x is greater than or equal to y, otherwise 0

==

Equal to

x == y

1 if x is equal to y, otherwise 0

!=

Not equal to

x != y

1 if x is not equal to y, otherwise 0

Logical Operators
Logical operators normally take relational expressions as operands. The ! operator takes one operand. The rest take two: one to the left, and one to the right.You can connect expressions using logical operators to form compound conditions, such as those often used in jump and loop statements to control the program flow. C uses the symbols described in boolean operations AND, OR, and NOT.

Operator

Meaning

Example

Result (1 = true, 0 = false)

&&

logical AND

x && y

1 if each of the operands x and y is not equal to zero, otherwise 0

||

logical OR

x || y

0 if each of x and y is equal to zero, otherwise 1

!

logical NOT

!x

1 if x is equal to zero, otherwise 0

Bitwise Operators
C programs can store information in individual bits or groups of bits. File access permissions are a common example. The bitwise operators allow you to manipulate individual bits in a byte or in a larger data unit: you can clear, set, or invert any bit or group of bits. You can also shift the bit pattern of an integer to the left or right.
The bit pattern of an integer type consists of bit positions numbered from right to left, beginning with position 0 for the least significant bit. For example, consider the char value '*', which in ASCII encoding is equal to 42, or binary 00101010 and bit position is 76543210.

Operator

Meaning

Example

Result (for each bit position)

(1 = set, 0 = cleared)

&

Bitwise AND

x & y

1, if 1 in both x and y

0, if 0 in x or y, or both

|

Bitwise OR

x | y

1, if 1 in x or y, or both

0, if 0 in both x and y

^

Bitwise

exclusive OR

x ^ y

1, if 1 either in x or in y, but not in both

0, if either value in both x and y

~

Bitwise NOT

(one's complement )

~x

1, if 0 in x

0, if 1 in x

The bitwise AND, OR, and NOT (one's complement) are governed by the same truth table as their logical equivalents, except that they work bit by bit. The exclusive OR has the truth table shown here:
p
q
p^q
0
0
0
1
0
1
1
1
0
0
1
1
Parity is often indicated by the eighth bit, which is set to 0 by ANDing it with a byte that has bits 1 through 7 set to 1 and bit 8 set to 0.The net result is that the eighth bit of ch is set to 0. In the following example, assume that ch had received the character A and had the parity bit set:

Memory Addressing Operators
The five operators listed in Table are used in addressing array elements and members of structures, and in using pointers to access objects and functions.

Operator

Meaning

Example

Result

&

Address of

&x

Pointer to x

*

Indirection operator

*p

The object or function that p points to

[ ]

Subscripting

x[y]

The element with the index y in the array x (or the element with the index x in the array y: the [ ] operator works either way)

.

Structure or union member designator

x.y

The member named y in the structure or union x

->

Structure or union member designator by reference

p->y

The member named y in the structure or union that p points to

Conditional Operator
The conditional operator is C's only ternary operator, which requires three operands (as opposed to the unary's single and the binary's double operand requirements). The conditional operator is used to replace if-else logic in some situations. The conditional operator is a two-part symbol, ?:, whose format follows:
conditional_expression ? expression1 : expression2;
Example: - minimum = (var1 < var2) ? var1 : var2;


Free Web Hosting