# Operators in C, Part 4

Programming

Share:

😳 Ever felt a little embarassed sharing your JavaScript? Let's fix it.

This article is the last in the series on Operators in C, following on from Operators in C Part 3. It will discuss the remaining operators. The next category is that of the equality operators which are closely assosciated with the relational operators.

## Equality Operators

There are two equality operators in C:

1. Equal to (==)

2. Not Equal to (!=)

These operators are also used to form logical expressions representing conditions that are either true or false. The value of the resulting expressions will be of type integer. The data type of the operands of a logical expression that are compared using equality operators must satisfy one of the following combinations:

a. Both the operands must be of arithmetic type.

b. Both the operands must be of pointer type.

Two pointers compare equal if:

a. Both are null pointers.

b. Both are pointers to the same object or function.

c. Both are pointers to one past the last element of the same array object.

d. One is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

## Bitwise AND, Bitwise Exclusive OR and Bitwise Inclusive OR Operators

There are three logical bitwise operators: *bitwise AND *(** &** ), *bitwise exclusive OR *( **^** )*, bitwise inclusive OR *( **| **)*.* Each of these operators requires two integer type operands. The operations are carried out independently on each pair of corresponding bits within the operands. Thus, the least significant bits (ie the rightmost bits) within the two operands will be compared first, then the next least significant bits, and so on, until all of the bits have been compared. The results of these comparisons are:

- A
**bitwise AND**expression will return a 1 if both bits have a value of 1 (ie if both the bits are true), otherwise it will return a value of 0. - A
**bitwise exclusive OR**expression will return a 1 if one of the bits has a value of 1 and the other has a value of 0 (ie if one bit is true and the other is false), otherwise it will return a value of 0. - A
**bitwise inclusive OR**expression will return a 1 if one or both of the bits have a value of 1 (ie if one or both the bits are true), otherwise it will return a value of 0.

Example : Suppose `a`

and `b`

are unsigned integer variables, where

`a = 0110 1101 1011 0111`

`b = 1010 0111 0010 0110`

____________________

Then,` a&b = 0010 0101 0010 0110 `

(result of bitwise AND)

Again, ` a^b = 1100 1010 1001 0001 `

(result of bitwise exclusive OR)

Also, ` a|b = 1110 1111 1011 0111`

(result of bitwise inclusive OR)

## Logical AND, Logical OR Operators

The Logical AND (&&) and the Logical OR (||) operators act upon operands that are themselves logical expressions. The net effect is to combine the individual logical expressions into more complex conditions that are either *true* or *false*.

The operands of a Logical AND expression and a Logical OR expression should be of **scalar type** (arithmetic types and pointer types). The result of a Logical AND expression and that of a Logical OR expression is of type` int`

.

The **Logical AND &&** operator shall yield 1 if both of its operands compare unequal to 0 (ie both the operands are true); otherwise, it yields 0. If the first operand compares equal to 0 (ie if the first operand is false), the second operand is not evaluated. This is because unlike the bitwise operators, operands of a logical expression are evaluated from left to right.

Example: Suppose` i`

is an integer variable whose value is `3`

and` c`

is a character variable whose value is` w`

. Then in the expression ( (i>=6)&&(c==’w’)) ; as the first operand `(i>=6)`

is false the second operand is not evaluated and the result of this Logical AND expression is 0.

The **Logical OR || **operator shall yield 1 if either of its operands compare unequal to 0 (ie if either of the operands are true); otherwise, it yields 0.

## The Conditional Operator (?:)

The conditional operator (?:) is a ternary operator, ie it requires three operands. Simple conditional operations can be carried out with the conditional operator. An expression that makes use of the conditional operator is called a **conditional expression. **Such an expression can be written in place of the if/else statement (which will be discussed in a later article).

A conditional expression is written in the form:

expression1 ? expression2 : expression3

When evaluating a conditional expression, expression1 is evaluated first. If expression1 is true, then expression2 is executed, else expression3 is executed.

If expression1 is true, expression2 is evaluated and this becomes the value of the conditional expression else expression3 is evaluated and this becomes the value of the conditional expression. The value of the conditional expression thus determined can be assigned to a variable and used later in the program. Conditional expressions frequently appear on the right hand side of a simple assignment statement. The resulting value of the conditional expression is assigned to the identifier on the left.

Example1: Suppose `i`

and `flag`

are two integer type variables. Consider the following conditional expression:

`(i<0)? 0:100;`

The expression`(i<0)`

is evaluated first. If it is true, (ie i is less than 0), the entire conditional expression takes on the value `0`

. Otherwise, the entire conditional expression takes on the value `100`

. The value of the conditional expression, thus determined, can be assigned to a variable and used later in the program. This is illustrated in the following statement:

`flag= (i<0)? 0:100;`

If` i`

is less than `0`

the value of `flag `

will be `0`

else it will be `100`

.

Example2: Let `a`

,` b`

and `max`

be three integer variables. The following conditional expression determines the larger value by comparing a and b and assigns it to max:

`max=(a>b)?a:b;`

## Assignment Operators

There are several different assignment operators in C. They are used to form **assignment expressions**, which assign the value of an expression to an identifier. The most commonly used assignment operator is the **simple assignment operator (=).** Assignment expressions that make use of this operator are written in the form:

identifier = expression

where *identifier *represents a variable, and *expression *represents a constant, a variable or a more complex expression.

Note that the *simple assignment operator* (=) and the *equality operator* (==) are distinctly different. The assignment operator is used to assign a value to an identifier, whereas the equality operator is used to determine if the two expressions have the same value. These operators cannot be used in place of one another.

**Assignment expressions **are often referred to as **assignment statements**, since they are actually written as complete statements. However, assignment expressions can also be written as expressions that are included within other statements.

If the two operands in an assignment expression are of different data types, then the value of the expression on the right will automatically be converted to the type of the identifier on the left. The entire expression will be of this same data type.

**Multiple Assignments **of the form

identifier1=identifier2=…..=expression

are permissible in C. Therefore the muliple assignment

identifier1= identifier2= expression

is equivalent to

identifier1= (identifier2= expression)

For example, in the statement `i=j=5;`

,where `i`

and `j`

are integer variables, the value `5`

is first assigned to `j `

and then to` i`

. Thus both variables are assigned the value `5`

in a single assignment statement by using multiple assignments.

### Compound Assignments

C contains the following compound assignment operators:

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

An expression using a compound assignment operator takes the form:

expression1 operator=expression2

To see how they are used consider the assignment operator +=. The assignment expression

expression1 += expression2

is equivalent to

expression1 = expression1 + expression2

For example:

`a+=2;`

is equivalent to

`a=a+2;`

The other compound assignment operators are also used in the same way. Usually expression1 is an identifier, such as a variable or an array element.

For the operators += and -= only, either the left operand shall be a pointer type and the right shall have integer type, or both the operands shall have arithmetic type.

For the other operators, each operand shall have arithmetic type consistent with those allowed by the corresponding binary operator.

## The Comma Operator

The** comma operator (,)** in C is a binary operator that acts as a *sequence point *between the two operands. It evaluates its first operand and then evaluates the second operand. The result of an expression using the comma operator has the value and data type of the second operand of the comma operator.

The use of the comma as an operator is different from its use in function calls, variable declarations, initializations etc. where it is used as a **separator.**

The following example illustrates the use of the comma operator:

Let `result`

, `a`

and `i`

be integer type variables. Consider the following expressions:

`i=2,a=3;`

`result = (i+=2,i+a);`

In the first statement` i=2,a=3;`

the comma is used as a separator. In the second statement, the comma operator first increments the value of` i`

by `2`

and then adds the value of` a(3)`

to `i(4)`

. The value stored in the variable` result`

is the value of the second operand `i+a,`

ie `7`

. If the parentheses are removed the value of the variable result will be `4`

. This is because the precedence of the assignment operator is higher than that of the comma operator. Thus, only the first operand` i+=2`

will be evaluated and its value will be stored in the variable `result`

. The use of parentheses raises the precedence of the operator within the parentheses.

The comma operator is also used in the for loop. The use of the comma operator in the for loop will be discussed in a later article when we discuss control structures.

## Operator Precedence

C specifies the precedence of operators in the evaluation of an expression which is highest precedence first. The following lists the precedence and assosciativity of the operator categories mentioned earlier. As already mentioned the use of parentheses raises the precedence of the operator within parentheses. The associativity of operators defines the order in which an operator evaluates its operands. For example: bitwise shift operators evaluate from right to left but logical operators from left to right.

### Precedence Operator group Associativity

1 Postfix operators Left to right

2 Prefix operators Right to left

3 Multiplicative operators Left to right

4 Additive operators Left to right

5 Bitwise shift operators Left to right

6 Relational operators Left to right

7 Equality operators Left to right

8 Bitwise operators Left to right

9 Logical operators Left to right

1o Conditional operator Left to right

11 Assignment operators Right to left

12 Comma operator Left to right

The next article will discuss the functions provided in C to read and write data.