Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Basic Operators and Their Precedence

C language provides various operators like arithmetic, relational, logical, bitwise and assignment operators. The document describes each operator type, provides examples and explains operator precedence which determines the order of evaluation in expressions. Operators with higher precedence like *, / and % are evaluated before lower precedence ones like + and -.

Uploaded by

mit rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Basic Operators and Their Precedence

C language provides various operators like arithmetic, relational, logical, bitwise and assignment operators. The document describes each operator type, provides examples and explains operator precedence which determines the order of evaluation in expressions. Operators with higher precedence like *, / and % are evaluated before lower precedence ones like + and -.

Uploaded by

mit rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Operators and Their Precedence

An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulations. C language is rich in built-in
operators and provides the following types of operators:

Arithmetic Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Ternary Operators

Arithmetic Operators

Following table shows all the arithmetic operators supported by C language.


Assume variable A holds 10 and variable B holds 20 then:

Operator Description Example


A+B
+ Adds two operands will give
30
A - B will
- Subtracts second operand from the first
give -10
A*B
* Multiplies both operands will give
200
B/A
/ Divides numerator by de-numerator will give
2
B%A
Modulus Operator and remainder of after an
% will give
integer division
0
Increments operator increases integer value by A++ will
++
one give 11
Decrements operator decreases integer value by A-- will
--
one give 9
Relational Operators

Following table shows all the relational operators supported by C language.


Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


(A == B)
Checks if the values of two operands are equal
== is not
or not, if yes then condition becomes true.
true.
Checks if the values of two operands are equal
(A != B)
!= or not, if values are not equal then condition
is true.
becomes true.
Checks if the value of left operand is greater
(A > B) is
> than the value of right operand, if yes then
not true.
condition becomes true.
Checks if the value of left operand is less than
(A < B) is
< the value of right operand, if yes then condition
true.
becomes true.
Checks if the value of left operand is greater (A >= B)
>= than or equal to the value of right operand, if yes is not
then condition becomes true. true.
Checks if the value of left operand is less than or
(A <= B)
<= equal to the value of right operand, if yes then
is true.
condition becomes true.

Logical Operators

Following table shows all the logical operators supported by C language.


Assume variable A holds 1 and variable B holds 0, then:

Operator Description Example


Called Logical AND operator. If both the
(A && B)
&& operands are non-zero, then condition becomes
is false.
true.
Called Logical OR Operator. If any of the two
(A || B)
|| operands is non-zero, then condition becomes
is true.
true.
Called Logical NOT Operator. Use to reverses the !(A &&
! logical state of its operand. If a condition is true B) is
then Logical NOT operator will make false. true.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ are as follows:

p Q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The Bitwise operators supported by C language are listed in the following


table. Assume variable A holds 60 and variable B holds 13, then:

Operator Description Example


(A & B) will
Binary AND Operator copies a bit to the give 12,
&
result if it exists in both operands. which is
0000 1100
(A | B) will
Binary OR Operator copies a bit if it exists in give 61,
|
either operand. which is
0011 1101
Binary XOR Operator copies the bit if it is set (A ^ B) will
^
in one operand but not both. give 49,
which is
0011 0001
(~A ) will
give -61,
which is
Binary Ones Complement Operator is unary
~ 1100 0011
and has the effect of 'flipping' bits.
in 2's
complement
form.
A << 2 will
Binary Left Shift Operator. The left operands
give 240
<< value is moved left by the number of bits
which is
specified by the right operand.
1111 0000
A >> 2 will
Binary Right Shift Operator. The left
give 15
>> operands value is moved right by the number
which is
of bits specified by the right operand.
0000 1111

Assignment Operators
There are following assignment operators supported by C language:

Operator Description Example


C=A+
B will
Simple assignment operator, Assigns values assign
=
from right side operands to left side operand value of
A+B
into C

Misc/Ternary Operators

Operator Description Example


If Condition is true ?
?: Conditional Expression Then value X :
Otherwise value Y

Operators Precedence
Operator precedence determines the grouping of terms in an expression. This
affects how an expression is evaluated. Certain operators have higher
precedence than others; for example, the multiplication operator has higher
precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator *
has higher precedence than +, so it first gets multiplied with 3*2 and then
adds into 7.

Here, operators with the highest precedence appear at the top of the table,
those with the lowest appear at the bottom. Within an expression, higher
precedence operators will be evaluated first.

Category Operator Associativity


Postfix () [] ++ - - Left to right
Unary + - ! ~ ++ - - Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left

You might also like