• admin@embedclogic.com

C-Operators

Operators perform any kind of operation like arithmetic, logical operation etc. Operators are divided into some categories according to its function.

Example:

sum=4+5  —> Here + and = are operators.

+ operator used for addition of 4 & 5 and result is assigned in sum by using = operator.

Classification of Operators

There are following categories on the basis of operators functionality –

Arithmetic operators

Relational Operators

Logical operators

Bit-wise operators

Compound Assignment operators

Pointer and Indirection operators

Arithmetic Operators

See the table for the various arithmetic operator:

Arithmetic OperatorSymbolExample useRemark
Addition+a+ba and b are two variable
Subtraction-a-ba and b are two variable
Multiplication*a*ba and b are two variable
Division/a/ba and b are two variable
modulus%a%ba and b are two variable
Assignment=a=ba and b are two variable

We are very familiar with almost all arithmetic operators use.I feel division and Modulus operators create confusion sometimes. So let us have a quick look at Division and Modulus operator.

Division and Modulus operator

Division operator gives quotient when one quantity is divided by another while Modulus gives remainder.

Example

case 1:   if a> b; a and b are integer variable

case 2:   if a<b; a and b are integer variable

case 3:   if a=b; a and b are integer variable

case 4:  On Float variables modulus operation is invalid

Relational/Comparision operator

The relational operators are used in the conditional statements to perform some operation.

See the table for relational operators list:

Comparision operatorsSymbolExample use
equal equal to ==a==b
not equal to!=a!=b
greater than>a>b
less than<a
greater than or equal to>=a>=b
less than or equal to<=a<=b

Logical Operators

Logical operators work between the true(1) and false(0) condition. Logical operators use with the Relational Operator or conditional operator. There are mainly three logical operators –

logical AND ( && )

logical OR    ( || )

logical NOT  ( ! )

Logical AND(&&):

Logical AND is used between the conditions when all conditions need to be true to perform some task.

Suppose there are two conditions Condition1 and Condition2, let us see logical AND(&&) operator use between these conditions.

Note: All Non-Zero number whether it is -ve or +ve, it is treated as True and only Zero consider as false.

Example

4&&5 =1(true)

4&&0 = 0(False)

-1&&1 = 1(true)

-4&&1=1(true)

(9>8)&&(4<5)=1(true)

Logical OR(||)

Logical OR operator are used between the conditions when only one condition need to be true to perform some task.

Table for logical OR operator is –

Example:

4||5 =1(true)

4||0 = 1(true)

1||0 = 1(true)

-4||1=1(true)

(9>8)||(4>5)=1(true)

0||0 = False(0)

Logical NOT(!)

logical NOT works only single condition.It negate the current status of condition.

! (TRUE) = 0

! (False) = 1

Program For Logical Operation

Bit-wise Operator

Logical operator considers whole digit as 1 or 0 and gives output in 1 and 0 i.e True and False while Bit-wise operator operates on every bit of a number.

Example

See the Table for Bitwise Operators –

Bit-wise OperatorSymbolExample use
bit-wise AND& a&b
bit-wise OR| a|b
bit-wise XOR^ a^b
Bit-wise left Shift<< a<
bit-wise right Shift>> a>>b
Bitwise Operation table
Bit-wise AND
Bit-wise OR
Bit-wise XOR

Example

E.g 1:

E.g 2:

E.g 3:

E.g 4:

Program For Bitwise Operation

Compound Assignment operator

Compound assignment operators are the combination of two operations. the first operation may be any arithmetic or bitwise operation but second must be assignment operation.

So instead of writing two separate statement for two operations, we can finish it by using single Compound assignment operators.

For Example, Perform addition of a and b and assign that sum to a.

we have two methods for this:

a=a+b;

a+=b; (+= compound addition assignment operator)

Compound assignment operators and their syntax:
Compound assignment operators.SymbolExample UseEquivalent Operation
Addition assignment+=a+=ba=a+b
Subtraction assignment-=a-=ba=a-b
Multiplication assignment*=a*=ba=a*b
Division assignment/=a/=ba=a/b
modulo assignment%=a%=ba=a%b
bit-wise AND assignment&=a&=ba=a&b
bit-wise OR assignment|=a|=ba=a|b
bit-wise XOR assignment^=a^=ba=a^b
bit-wise Left Shift assignment<<=a<<=ba=a<
bit-wise Right shift assignment>>=a>>=ba=a>>b

Other operators

Ternary Operator

The ternary operator is the combination of three operators- relational,? and : so it is known as the ternary operator.

Syntax:

If the condition is true then statment1 will execute otherwise statment2 if the condition is false.

Example: