0% found this document useful (0 votes)
11 views

The Operators in C

Jrjrjrrj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

The Operators in C

Jrjrjrrj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The Operators in C

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform
addition.

C has a wide range of operators to perform various operations.

The Assignment Operator

The assignment operator is represented by the equal sign(=). This is used to assign a value to a variable.

Example:
a=5; /* the value 5 is assigned to a*/
b=a; /*the value of a is stored to b*/
c=a+b; /*the sum of a and b is stored to c*/
d=e=0; /*this will assign zero as the value of e and d */

Arithmetic Operators in C

The Arithmetic operators are some of the C Programming Operator, which are used to perform arithmetic
operations includes operators like Addition, Subtraction, Multiplication, Division and Modulus.

All these Arithmetic operators in C are binary operators which means they operate on two operands.

Below table shows all the Arithmetic Operators in C Programming with examples.

ARITHMETIC
OPERATION EXAMPLE
OPERATORS IN C

+ Addition 10 + 2 = 12

– Subtraction 10 – 2 = 8

* Multiplication 10 * 2 = 20

/ Division 10 / 2 = 5

Modulus – It returns the remainder 10 % 2 = 0 (Here remainder is


%
after the division zero). If it is 10 % 3 then it will be 1.
Relational Operators in C

Relational operators are used to compare values of two expressions. Relational operators are binary
operators because they require two operands to operate. An expression which contains the relational
operators is called relational expression. If the relation is true then the result of the relational expression
is 1, if the relation is false then the result of the relational expression is 0.

The following table lists relational operators along with some examples:
Operator Description Example Result
> Greater than 1>2 0
>= Greater than or equal to 3 >= 2 1
< Smaller than 10 < 5 0
<= Smaller than or equal to 6 <= 7 1
== equal to 98==98 1
!= not equal to 10 != 9 1

In C, all non-zero values are considered as true while 0 is considered as false.

Precedence

The precedence of <, <=, > and >= operators are same and they associate from left to right. However, the
precedence of == and != is lower than other relational operators and they associate from left to right. The
precedence of relational operators is lower than the arithmetic operators.

To clear things up let’s evaluate some expressions involving relational operators:

Example 1:
4 + 2 * 3 > 12 - 2
Step 1: Evaluate 2 * 3.
4 + 6 > 12 - 2
Step 2: Evaluate 4 + 6 followed by 12 - 2.
10 > 10
Step 3: 10 is not greater than 10, so the above expression evaluates to false ( 0 ). Hence the result of the
entire expression is 0.
Result: 4+2*3>12-2=0

Example 2:

(4 % 2 == 0) <= (8 * 2)
Step 1: The parentheses operator has the highest precedence and it associates from left to right. So the
expression (4 % 2 == 0) will be evaluated first. The % operator has higher precedence than the equal
to == operator. Therefore, the % operator will be applied first followed by the == operator. The expression
now becomes:
(4 % 2 == 0) <= (8 * 2)
(0 == 0) <= (8 * 2)
1 <= (8 * 2)
Step 2: Evaluate (8 * 2).
1 <= (8 * 2)
1 <= 16
Step 3: 1 is smaller than 16. So the above expression evaluates to true ( 1 ). Hence the result of the entire
expression is true.

Don’t confuse assignment operator ( = ) with equal to operator ( == ). The first one is used to assign a
value to the variable while the second one is used to test whether two values are equal or not.
To use relational operators to its full potential you must learn how to use the if-else statement.

Increment and Decrement Operators in C

C has two special unary operators called increment (++) and decrement (--) operators. These operators
increment and decrement value of a variable by 1.
++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1
Increment and decrement operators can be used only with variables. They can’t be used with constants
or expressions.

int x = 1, y = 1;

++x; // valid

++5; // invalid - increment operator operating on a constant value

++(x+y); // invalid - increment operating on an expression


Increment/Decrement operators are of two types:

1. Prefix increment/decrement operator.


2. Postfix increment/decrement operator.

Let’s start with the first one.


Prefix increment/decrement operator
The prefix increment/decrement operator immediately increases or decreases the current value of the
variable. This value is then used in the expression.

Let’s take an example:

y = ++x;
Here first, the current value of x is incremented by 1. The new value of x is then assigned to y.
Similarly, in the statement:

y = --x;
the current value of x is decremented by 1. The new value of x is then assigned to y.
The following program demonstrates prefix increment/decrement operator in action:
LOGICAL OPERATORS IN C:
● These operators are used to perform logical operations on the given expressions.
● There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical
NOT (!).
Operators Example/Description

(x>5)&&(y<5)
&& (logical AND) It returns true when both conditions are true

(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true

!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT) If “((x>5) && (y<5))” is true, logical NOT operator makes it false

We can evaluate these two logical operators by using a truth table:


P q P&&Q PIq !p
1 1 1 1 0
1 0 0 1 0
0 1 0 1 1
0 0 0 0 1

Shorthand Operators are used to simplify coding of certain types of assignment statements.
Example:
x=x+10 in shorthand x+=10
The operator +=tells the compiler to assign x the value of x+10.
y=y-3 in shorthand y-=3.

You might also like