Structured Programming Language
Operators in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Operator
An operator is a program element that is applied to one or
more operands in an expression or statement.
• Operands can be a value or a variable.
• Operators that take
• one operand  Unary operators.
• two operands  Binary operators.
• three operands  Ternary operators.
 Example:
5 + 7
Or,
a + b where a , b are two variables.
Types of Operators
C Arithmetic Operators
Operator Purpose
+ Adds two operands
- Subtracts second operand from the first
* Multiplies two operands
/ Divides numerator by denominator
% Remainder after integer division
Examples
a= 10, b=3 result v1= 12.5,
v2 = 2.0
result c1=‘P’
c2 = ‘T’
result
a+b 13 v1+v2 14.5 c1 80
a-b 7 v1-v2 10.5 c1+c2 164
a*b 30 v1*v2 25.0 c1 + c2 +5 169
a/b 3 v1/v2 6.25 c1+c2+’5’ 217
a%b 1
 If one or both operands represent negative values,
then the addition, subtraction, multiplication and
division operations will result in values whose signs
are determined by the usual rules of algebra.
 For remainder, most versions of C assign the sign of
the first operand to the remainder to fulfill the next
condition
dividend = (integer quotient)*divisor + remainder
i.e.
a = ((a/b)*b)+(a%b)
will always be satisfied.
Values Results
11 % 3 2
11 % -3 2
-11 % 3 -2
-11 % -3 -2
Examples
Type Conversion
• The data type is promoted from lower to higher.
char
short
unsigned
short
int
unsigned
int
long
unsigned
long
long long
unsigned
long long
float
double
long
double
Example:
int i=7;
double d=5.5;
char c=‘w’;
Then the data type of
(i+c)-(2*d/5)
is double and the result is
123.8
Type Casting
• Explicit type conversion can be forced in any expression, with
a unary operator called a cast.
• Syntax: (type) expression
• Example: int n;
float x;
x=(float) n;
• The above statement will convert the value of n to a float
value before assigning to x, but n is not altered.
• Type casting doesn’t change the actual value of the variable
but the resultant value may be put in temporary storage.
Precedence(order of evaluation)
• The operators within C are grouped hierarchically according to
their precedence.
• Operations with a higher precedence are carried out before
operations having a lower precedence.
• Example:
2 precedence groups for arithmetic operators.
Group 1(higher precedence): *, /, %
Group 2(lower precedence): +, -
Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left  Right
a – b / c * d
Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left  Right
≡ a – [(b/c) * d]a – b / c * d
Code Sample
Unary Operators
• Operators that act upon a single operand to produce a new
value
• Same precedence group
• Associativity Right  Left
Operator Purpose Example
- To negate a numerical constant,
variable or expression
-743, -0X7FFF,-0.2, -5E-8,
-var1, -(x+y), -3*(x+y)
++ Pre/Post increment operator ++a, a++
-- Pre/Post decrement operator --a, a--
sizeof() Returns size of its operand in bytes sizeof(int)
(type) Type casting (float) 5
Code Sample
c=a++; /// c=a; a=a+1;
c=++a; /// a=a+1; c=1;
Relational Operators
Operator Purpose
< Check if operand on the left is smaller than operand on the right
<= Check if operand on the let is smaller than or equal to right operand
> Check if operand on the left is greater than operand o the right
>= Check left operand is greater than or equal to right operand
• Same precedence group
• Associativity Left  Right
Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left  Right
Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left  Right
The resulting value of both relational and equality operators is
either TRUE(integer 1) or FALSE(integer 0)
Examples
Expression(i=1,j=2,k=3) Interpretation Value
i < j true 1
(i + j) >= k true 1
(j + k) > (i + 5) false 0
k != 3 false 0
j == 2 true 1
Code Sample
Logical Operators
Operator Purpose
&& Logical AND
|| Logical OR
!
(unary)
Logical NOT
• && has higher precedence than ||
• Associativity Left  Right
Examples
Expression(i=7,f=5.5,c=‘w’) Interpretation Value
(i>=6) && (c==‘w’) true 1
(i>=6) || (c==119) true 1
(f<11) && (i>100) false 0
(c!=‘p’) || ( (i+f) <= 10) true 1
! (i<=7) false 0
Code Sample
Assignment Operators
Operator Purpose
= Assigns values from the right side operands to left side operand
+= a += 5 is same as a=a+5
-= a += 5 is same as a=a-5
*= a += 5 is same as a=a*5
/= a += 5 is same as a=a/5
%= a += 5 is same as a=a%5
Assignment Operators
• Same precedence group
• Associativity Right  Left
• Example:
i = j = 5;
first assigns 5 to variable j, then assigns the value of j to i
Code Sample
Operator Purpose
~
(unary)
One’s compliment operator
(reverse each bits of operands i.e. 0  1 and 1  0)
& Bitwise AND
(perform and operations on each bits)
| Bitwise OR
(perform or operation on each bits)
^ Bitwise exclusive OR (XOR)
<< shift left
>> shift right
Bitwise Operators
• Associativity Left  Right
• Precedence : shift > and > xor > or
Truth table for bitwise & , | , ^
a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Examples
a = 0x6db7 = 0110 1101 1011 0111
~a = 0x9248 = 1001 0010 0100 1000
b = 0xa726 = 1010 0111 0010 0110
~b = 0x58d9 = 0101 1000 1101 1001
a = 0x6db7 = 0110 1101 1011 0111
b = 0xa726 = 1010 0111 0010 0110
a & b = 0xca91 = 0010 0101 0010 0110
Examples
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a << 6 ; // 0110 1101 1100 0000
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a >> 6 ; // ???? ???? ???? ????
Code Sample
Bitwise Assignment Operators
Operator Purpose
&= a &= 0x7f equivalents to a= a & 0x7f
^= a ^= 0x7f equivalents to a= a ^ 0x7f
|= a |= 0x7f equivalents to a= a | 0x7f
<<= a <<= 5 equivalents to a= a << 5
>>= a >>= 5 equivalents to a= a >> 5
• Associativity Right  Left
• Same precedence group
Conditional Operators
Operator
expression 1 ? expression 2 : expression 3
When evaluating expression, expression 1 is evaluated first.
If expression 1 is true, then expression 2 is evaluated and this
becomes the value of the conditional expression.
if expression 1 is false, then expression 3 is evaluated and this
becomes the value of the conditional expression.
Code Sample
• Write a program to find out whether a number is
even or odd.
Special operators
Operator Description
& Returns the address of an variable
Ex. Printf(“%#X n”, &x);
* Points to a memory location of specific data type
Ex. int *x;
, int a, b=10,c;
C operators precedence table
Category Operator Associativity
Postfix () [] -> . ++ -- L to R
Unary + - ! ~ ++ -- (type) * & sizeof() R to L
Multiplicative * / % L to R
Additive + - L to R
Shift << >> L to R
Relational < <= > >= L to R
Equality == != L to R
Bitwise AND & L to R
Bitwise XOR ^ L to R
Bitwise OR | L to R
Logical AND && L to R
Logical OR || L to R
Conditional ? : R to L
Assignment = += -= *= /= %= >>= <<= &= ^= |= R to L
Comma , L to R
References
• Schaum’s outlines “Programming with C” by Byron
Gottfried (3rd edition), Chapter 3 (full)
• https://www.programiz.com/c-programming/c-
operators
• https://www.tutorialspoint.com/cprogramming/c_oper
ators.htm

SPL 6 | Operators in C

  • 1.
    Structured Programming Language Operatorsin C Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2.
    Operator An operator isa program element that is applied to one or more operands in an expression or statement. • Operands can be a value or a variable. • Operators that take • one operand  Unary operators. • two operands  Binary operators. • three operands  Ternary operators.  Example: 5 + 7 Or, a + b where a , b are two variables.
  • 3.
  • 4.
    C Arithmetic Operators OperatorPurpose + Adds two operands - Subtracts second operand from the first * Multiplies two operands / Divides numerator by denominator % Remainder after integer division
  • 5.
    Examples a= 10, b=3result v1= 12.5, v2 = 2.0 result c1=‘P’ c2 = ‘T’ result a+b 13 v1+v2 14.5 c1 80 a-b 7 v1-v2 10.5 c1+c2 164 a*b 30 v1*v2 25.0 c1 + c2 +5 169 a/b 3 v1/v2 6.25 c1+c2+’5’ 217 a%b 1
  • 6.
     If oneor both operands represent negative values, then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.  For remainder, most versions of C assign the sign of the first operand to the remainder to fulfill the next condition dividend = (integer quotient)*divisor + remainder i.e. a = ((a/b)*b)+(a%b) will always be satisfied.
  • 7.
    Values Results 11 %3 2 11 % -3 2 -11 % 3 -2 -11 % -3 -2 Examples
  • 8.
    Type Conversion • Thedata type is promoted from lower to higher. char short unsigned short int unsigned int long unsigned long long long unsigned long long float double long double Example: int i=7; double d=5.5; char c=‘w’; Then the data type of (i+c)-(2*d/5) is double and the result is 123.8
  • 9.
    Type Casting • Explicittype conversion can be forced in any expression, with a unary operator called a cast. • Syntax: (type) expression • Example: int n; float x; x=(float) n; • The above statement will convert the value of n to a float value before assigning to x, but n is not altered. • Type casting doesn’t change the actual value of the variable but the resultant value may be put in temporary storage.
  • 10.
    Precedence(order of evaluation) •The operators within C are grouped hierarchically according to their precedence. • Operations with a higher precedence are carried out before operations having a lower precedence. • Example: 2 precedence groups for arithmetic operators. Group 1(higher precedence): *, /, % Group 2(lower precedence): +, -
  • 11.
    Associativity • The orderin which consecutive operations within the same precedence group are carried out. • Example: each of the 2 precedence groups of arithmetic operators has associativity from Left  Right a – b / c * d
  • 12.
    Associativity • The orderin which consecutive operations within the same precedence group are carried out. • Example: each of the 2 precedence groups of arithmetic operators has associativity from Left  Right ≡ a – [(b/c) * d]a – b / c * d
  • 13.
  • 14.
    Unary Operators • Operatorsthat act upon a single operand to produce a new value • Same precedence group • Associativity Right  Left Operator Purpose Example - To negate a numerical constant, variable or expression -743, -0X7FFF,-0.2, -5E-8, -var1, -(x+y), -3*(x+y) ++ Pre/Post increment operator ++a, a++ -- Pre/Post decrement operator --a, a-- sizeof() Returns size of its operand in bytes sizeof(int) (type) Type casting (float) 5
  • 15.
    Code Sample c=a++; ///c=a; a=a+1; c=++a; /// a=a+1; c=1;
  • 16.
    Relational Operators Operator Purpose <Check if operand on the left is smaller than operand on the right <= Check if operand on the let is smaller than or equal to right operand > Check if operand on the left is greater than operand o the right >= Check left operand is greater than or equal to right operand • Same precedence group • Associativity Left  Right
  • 17.
    Equality Operators Operator Purpose ==Check if two operands are equal != Check if two operands are not equal • Same precedence group • Associativity Left  Right
  • 18.
    Equality Operators Operator Purpose ==Check if two operands are equal != Check if two operands are not equal • Same precedence group • Associativity Left  Right The resulting value of both relational and equality operators is either TRUE(integer 1) or FALSE(integer 0)
  • 19.
    Examples Expression(i=1,j=2,k=3) Interpretation Value i< j true 1 (i + j) >= k true 1 (j + k) > (i + 5) false 0 k != 3 false 0 j == 2 true 1
  • 20.
  • 21.
    Logical Operators Operator Purpose &&Logical AND || Logical OR ! (unary) Logical NOT • && has higher precedence than || • Associativity Left  Right
  • 22.
    Examples Expression(i=7,f=5.5,c=‘w’) Interpretation Value (i>=6)&& (c==‘w’) true 1 (i>=6) || (c==119) true 1 (f<11) && (i>100) false 0 (c!=‘p’) || ( (i+f) <= 10) true 1 ! (i<=7) false 0
  • 23.
  • 24.
    Assignment Operators Operator Purpose =Assigns values from the right side operands to left side operand += a += 5 is same as a=a+5 -= a += 5 is same as a=a-5 *= a += 5 is same as a=a*5 /= a += 5 is same as a=a/5 %= a += 5 is same as a=a%5
  • 25.
    Assignment Operators • Sameprecedence group • Associativity Right  Left • Example: i = j = 5; first assigns 5 to variable j, then assigns the value of j to i
  • 26.
  • 27.
    Operator Purpose ~ (unary) One’s complimentoperator (reverse each bits of operands i.e. 0  1 and 1  0) & Bitwise AND (perform and operations on each bits) | Bitwise OR (perform or operation on each bits) ^ Bitwise exclusive OR (XOR) << shift left >> shift right Bitwise Operators • Associativity Left  Right • Precedence : shift > and > xor > or
  • 28.
    Truth table forbitwise & , | , ^ a b a & b a | b a ^ b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
  • 29.
    Examples a = 0x6db7= 0110 1101 1011 0111 ~a = 0x9248 = 1001 0010 0100 1000 b = 0xa726 = 1010 0111 0010 0110 ~b = 0x58d9 = 0101 1000 1101 1001 a = 0x6db7 = 0110 1101 1011 0111 b = 0xa726 = 1010 0111 0010 0110 a & b = 0xca91 = 0010 0101 0010 0110
  • 30.
    Examples int a =0x6db7 ; // 0110 1101 1011 0111 b = a << 6 ; // 0110 1101 1100 0000 int a = 0x6db7 ; // 0110 1101 1011 0111 b = a >> 6 ; // ???? ???? ???? ????
  • 31.
  • 32.
    Bitwise Assignment Operators OperatorPurpose &= a &= 0x7f equivalents to a= a & 0x7f ^= a ^= 0x7f equivalents to a= a ^ 0x7f |= a |= 0x7f equivalents to a= a | 0x7f <<= a <<= 5 equivalents to a= a << 5 >>= a >>= 5 equivalents to a= a >> 5 • Associativity Right  Left • Same precedence group
  • 33.
    Conditional Operators Operator expression 1? expression 2 : expression 3 When evaluating expression, expression 1 is evaluated first. If expression 1 is true, then expression 2 is evaluated and this becomes the value of the conditional expression. if expression 1 is false, then expression 3 is evaluated and this becomes the value of the conditional expression.
  • 34.
    Code Sample • Writea program to find out whether a number is even or odd.
  • 35.
    Special operators Operator Description &Returns the address of an variable Ex. Printf(“%#X n”, &x); * Points to a memory location of specific data type Ex. int *x; , int a, b=10,c;
  • 36.
    C operators precedencetable Category Operator Associativity Postfix () [] -> . ++ -- L to R Unary + - ! ~ ++ -- (type) * & sizeof() R to L Multiplicative * / % L to R Additive + - L to R Shift << >> L to R Relational < <= > >= L to R Equality == != L to R Bitwise AND & L to R Bitwise XOR ^ L to R Bitwise OR | L to R Logical AND && L to R Logical OR || L to R Conditional ? : R to L Assignment = += -= *= /= %= >>= <<= &= ^= |= R to L Comma , L to R
  • 37.
    References • Schaum’s outlines“Programming with C” by Byron Gottfried (3rd edition), Chapter 3 (full) • https://www.programiz.com/c-programming/c- operators • https://www.tutorialspoint.com/cprogramming/c_oper ators.htm