C Language
Operators
Engr. Qazi Shahzad Ali
C Language Operators
 Arithmetic Operators
 Increment Operator
 Decrement Operator
 Relational Operator
 Logical Operator
 Equality Operator
 Assignment Operator
 Conditional Operator
 Arithmetic expression
2
1-Arithmetic Operators
Operator Symbol Example
Multiplicatio
n
* a*b
Division / a/b
Addition + a+b
Subtraction - a-b
Modulus % a%b
3
Example for integers:
Exampl
e
Result
a*b 30
a/b 3
a+b 13
a-b 7
a%b 1
a=10
b=3
4
Example for floating point variable:
Exampl
e
Result
a*b 25.0
a/b 6.25
a+b 14.5
a-b 10.5
a%b NA
a=12.5
b=2.0
5
Example for Character:
Example Result
C1 80
c1+c2 164
c1+c2+5 169
c1+c2+
‘5’
217
Char C1,
C2
C1=‘T’
C2=‘P’
The ASCII code of ‘5’ is 53
6
2-Increment & Decrement Operators:
Operator Symbol Example
Increment ++ a++ or ++a
Decrement -- a-- or --a
7
2-Increment & Decrement Operators:
Operator Symbol Example
Increment ++ a++ or ++a
Decrement -- a-- or --a
8
3-Relational Operators
(use in decision making)
Operator
Symbo
l
Example
less than < a<b
greater than > a>b
less than or equal <= a<=b
greater than or
equal
>= a>=b
equal == a==b
Not equal != a!=b9
Example: i=1, j=2, k=3
Expression Meaning Value
i<j Correct 1
(i+j)>=k Correct 1
(j+k) > (i+5) wrong 0
K!=3 wrong 0
J==2 Correct 1
10
4-Logical Operator
Operator Symbol Example
AND && a<b && c>b
OR || a<b || c>b
NOT ! !(a<b)
11
Example: integer=i=7, float=f=5.5, character=c=‘w’
Expression Meaning Value
(i>=6)&&(c==‘w’) Correct 1
(i>6)||(c==119) Correct 1
(f<11)&&(i>100) wrong 0
(c!=‘p’)||(i+f)<=10 Correct 1
12
Operant Result
x y !x !y x&&y x||y
0 0 1 1 0 0
0
Non-
zero
1 0 0 1
Non-
zero
0 0 1 0 1
Non-
zero
Non-
zero
0 0 1 1
Logical Operators: AND, OR, NOT
13
Example: i=7, f=5.5
Expression Alternative expression Meanings
f>5 correct 1
!(f>5) wrong 0
i<=3 wrong 0
!(i<=3) correct 1
i>(f+1) correct 1
!(i>(f+1)) wrong 0
14
Bitwise Operator
Operator Symbol Example
AND & a&b
OR | (Pipe) a|b
Exclusive OR ^ (Caret) a^b
Complement ~ (Tilde) ~a
Right Shift >> a>>b
Left Shift << a<<b
15
Address and Indirection Operator:
Operator Symbol Example
Address & addr=&var
Indirection * Value=*addr
16
Memory Location:
3
i
6552
4
Location Name
Value at Location
Location Number
17
Conditional Operator
Generic Expression Example
result=(expression) ? Value1: value 2; max=(a>b) ? a:b;
If the expression is true (other than 0) then value 1 will be assigned to variable
“result”
If the expression is false (equal to 0) then value 2 will be assigned to variable “result”
18
Sizeof Operator:
Generic
Expression
Example
sizeof ()
sizeof(int), sizeof (struct
emp)
19
Algebraic Expression:
Algebraic Expression C Expression
a x b- c x d A *b-c*d
(m+n)(a+b) (m+n)*(a+b)
3x2+2x+5 3*x*x+2*x+5
a+b+c/d+e (a+b+c)/(d+e)
[2by/d+1-x/3(z+y)] 2*b*y/(d+1)-x/3*(z+y)
20
Assignment Operators
Operator Symbol Example
Equal(assign the value, different from
==)
= a=b
addition += a+=b (same as a=a+b)
subtraction -= a-=b (same as a=a-b)
multiplication *= a*=b (same as a=a*b)
division /= a/+b (same as a=a/b)
remainder %= a%=b (same as a=a%b)
Bitwise AND &= a&=b (same as a=a&b)
Bitwise inclusive OR |= a|=b (same as a=a|b)
Bitwise exclusive OR ^= a^=b (same as a=a^b)
Left shift <<= a<<=2 (same as a=<<2)
Right shift >>= a>>=3 (same as a=>>3)
21
Example: Assignment Operator
Expression Alternative expression
Variable 1+= variable 2 Variable 1=variable1 +variable 2
Variable 1-= variable 2 Variable 1=variable1 -variable 2
Meanings: The variable 1 on left side will add value
of variable 2 on right side and the resultant new value
will be assign to variable 1
22
Integer and Float Conversions:
24
Escape Sequence:
Escape sequence is used in the printf() function to do something to
the output.
Escape
Sequence
Effect
a Beep sound, alarm
b Backspace
f Form feed (for printing)
n New line
r Carriage return (move the curser to beginning of current line)
t Tab
v Vertical tab
 Backslash (to print  at output)
” Used to print (“) sign
’ Used to print (‘) sign
27
Format Specifier
Tells the printf() function the format of the output to be printed put
No Format Specifier Output Type Output Example
1 %d or %i Signed decimal integer (short) 76
2 %u Unsigned decimal integer (short) 76
3 %o Unsigned octal integer 134
4 %x Unsigned hexadecimal (small letter) 9c
5 %X Unsigned hexadecimal (capital letter) 9C
6 %f Integer including decimal point 76.0000
7 %e Signed floating point (using e notation) 7.6000e+01
8 %E Signed floating point (using E notation) 7.6000E+01
9 %g The shorter between %f and %e 76
10 %G The shorter between %f and %E 76
11 %c Single Character (both for signed & unsigned) ‘7’
12 %s String “76”
28
Filed–Width Specifiers:
With the help of this
Interpretation of a variable’s type
Width of the field
Number of decimal places printed
Justification
29
Example # 1: Field-Width Specifer:
A g e i s 3 3 .
A g e i s 3 3 .
A g e i s 3 3 .
printf(“Age is %2d.”, age);
printf(“Age is %4d.”, age);
printf(“Age is %6d.”, age);
30
Example # 2: Field-Width Specifer:
main()
{
Float temp=27.25;
printf(“The temperature is %f.”, temp);
}
Output:
The Temperature is 27.250000
main()
{
Float temp=27.25;
printf(“The temperature is %.2f.”, temp);
}
Output:
The Temperature is 27.25
31
Study the following example:
main()
{
printf(“%.1f%8.1f%8.1fn”,3.0,12.5,523.3);
printf(“%.1f%8.1f%8.1fn”,300.0,1200.5,52300.3);
}
What will be the output??
32
Output of Previous Example:
 output
3.0 12.5 523.3
300.0 1200.5 52300.3
In other way
3 . 0 1 2 . 5 5 2 3 . 3
3 0 0 . 0 1 2 0 0 . 5 5 2 3 0 0 . 3
8 8
8 8
33
Study the following example:
main()
{
printf(“%-.1f%-8.1f%-8.1fn”,3.0,12.5,523.3);
printf(“%-.1f%-8.1f%-8.1fn”,300.0,1200.5,52300.3);
}
What will be the output??
34
Output of Previous Example:
 output
In other way
3 . 0 1 2 . 5 5 2 3 . 3
3 0 0 . 0 1 2 0 0 . 5 5 2 3 0 0 . 3
8 8
8 8
300.0 1200.5 52300.3
3.0 12.5 523.3
35
Explanation:
%8.1f
%
8 . 1
f
Signals format
specification
field width
# of digits to the
right of decimal
Indicates
decimal-format
floating point
36
Scanf and Assignment:
scanf:
It is used to save the function input into its relavent
variable.
Assignment operator:
Its takes answer of a statement and saves into other
variable
37
Hierarchy of Operation:
Priority operator Description
1st * / %
multiplication, division ,
modular division
2nd + - addition , subtraction
3rd = Assignment
38
Associativity of operators
 When two expression contains two operators of
equal priority the tie between them is settled using
the associativity of the operators .
1. Left to right (left operand must be unambiguous)
2. Right to left (right operand must be unambiguous)
Means it must not be involved in evaluation of any
other sub-expression
Example:
a=3/2*5
39
Example: a=3/2*5
Operator left right remark
/ 3 2 or 2*5
Left operand
is
unambiguous
, Right is not
* 3/2 or 2 5
Right
operand is
unambiguous
, Left is not
Meanings: Since both have / and * have L to R associativity and
Only / has unambiguous, left operand (necessary condition for L to R associativity
is performed earlier.
40
Operator Associativity Chart
44
Evaluation:
kk=3/2*4+3/8+3
Solution:
 kk=3/2*4+3/8+3
 kk=1*4+3/8+3 operation : /
 kk=4+3/8+3 operation : *
 kk=4+0+3 operation : /
 kk=4+3 operation : +
 kk=7 operation : +
45
46
Solve the Question
i=2*3/4+4/4+8-2+5/8
Evaluation Exercise:
 i=2*3/4+4/4+8-2+5/8
Solution:
 i=2*3/4+4/4+8-2+5/8
 i=6/4+4/4+8-2+5/8 operation : *
 i=1+4/4+8-2+5/8 operation : /
 i=1+1+8-2+5/8 operation : /
 i=1+1+8-2+0 operation : /
 i=2+8-2+0 operation : +
 i=10-2+0 operation : +
 i=8+0 operation : -
 i=8 operation : +
47
48

Operators in C Programming

  • 1.
  • 2.
    C Language Operators Arithmetic Operators  Increment Operator  Decrement Operator  Relational Operator  Logical Operator  Equality Operator  Assignment Operator  Conditional Operator  Arithmetic expression 2
  • 3.
    1-Arithmetic Operators Operator SymbolExample Multiplicatio n * a*b Division / a/b Addition + a+b Subtraction - a-b Modulus % a%b 3
  • 4.
    Example for integers: Exampl e Result a*b30 a/b 3 a+b 13 a-b 7 a%b 1 a=10 b=3 4
  • 5.
    Example for floatingpoint variable: Exampl e Result a*b 25.0 a/b 6.25 a+b 14.5 a-b 10.5 a%b NA a=12.5 b=2.0 5
  • 6.
    Example for Character: ExampleResult C1 80 c1+c2 164 c1+c2+5 169 c1+c2+ ‘5’ 217 Char C1, C2 C1=‘T’ C2=‘P’ The ASCII code of ‘5’ is 53 6
  • 7.
    2-Increment & DecrementOperators: Operator Symbol Example Increment ++ a++ or ++a Decrement -- a-- or --a 7
  • 8.
    2-Increment & DecrementOperators: Operator Symbol Example Increment ++ a++ or ++a Decrement -- a-- or --a 8
  • 9.
    3-Relational Operators (use indecision making) Operator Symbo l Example less than < a<b greater than > a>b less than or equal <= a<=b greater than or equal >= a>=b equal == a==b Not equal != a!=b9
  • 10.
    Example: i=1, j=2,k=3 Expression Meaning Value i<j Correct 1 (i+j)>=k Correct 1 (j+k) > (i+5) wrong 0 K!=3 wrong 0 J==2 Correct 1 10
  • 11.
    4-Logical Operator Operator SymbolExample AND && a<b && c>b OR || a<b || c>b NOT ! !(a<b) 11
  • 12.
    Example: integer=i=7, float=f=5.5,character=c=‘w’ Expression Meaning Value (i>=6)&&(c==‘w’) Correct 1 (i>6)||(c==119) Correct 1 (f<11)&&(i>100) wrong 0 (c!=‘p’)||(i+f)<=10 Correct 1 12
  • 13.
    Operant Result x y!x !y x&&y x||y 0 0 1 1 0 0 0 Non- zero 1 0 0 1 Non- zero 0 0 1 0 1 Non- zero Non- zero 0 0 1 1 Logical Operators: AND, OR, NOT 13
  • 14.
    Example: i=7, f=5.5 ExpressionAlternative expression Meanings f>5 correct 1 !(f>5) wrong 0 i<=3 wrong 0 !(i<=3) correct 1 i>(f+1) correct 1 !(i>(f+1)) wrong 0 14
  • 15.
    Bitwise Operator Operator SymbolExample AND & a&b OR | (Pipe) a|b Exclusive OR ^ (Caret) a^b Complement ~ (Tilde) ~a Right Shift >> a>>b Left Shift << a<<b 15
  • 16.
    Address and IndirectionOperator: Operator Symbol Example Address & addr=&var Indirection * Value=*addr 16
  • 17.
  • 18.
    Conditional Operator Generic ExpressionExample result=(expression) ? Value1: value 2; max=(a>b) ? a:b; If the expression is true (other than 0) then value 1 will be assigned to variable “result” If the expression is false (equal to 0) then value 2 will be assigned to variable “result” 18
  • 19.
  • 20.
    Algebraic Expression: Algebraic ExpressionC Expression a x b- c x d A *b-c*d (m+n)(a+b) (m+n)*(a+b) 3x2+2x+5 3*x*x+2*x+5 a+b+c/d+e (a+b+c)/(d+e) [2by/d+1-x/3(z+y)] 2*b*y/(d+1)-x/3*(z+y) 20
  • 21.
    Assignment Operators Operator SymbolExample Equal(assign the value, different from ==) = a=b addition += a+=b (same as a=a+b) subtraction -= a-=b (same as a=a-b) multiplication *= a*=b (same as a=a*b) division /= a/+b (same as a=a/b) remainder %= a%=b (same as a=a%b) Bitwise AND &= a&=b (same as a=a&b) Bitwise inclusive OR |= a|=b (same as a=a|b) Bitwise exclusive OR ^= a^=b (same as a=a^b) Left shift <<= a<<=2 (same as a=<<2) Right shift >>= a>>=3 (same as a=>>3) 21
  • 22.
    Example: Assignment Operator ExpressionAlternative expression Variable 1+= variable 2 Variable 1=variable1 +variable 2 Variable 1-= variable 2 Variable 1=variable1 -variable 2 Meanings: The variable 1 on left side will add value of variable 2 on right side and the resultant new value will be assign to variable 1 22
  • 23.
    Integer and FloatConversions: 24
  • 24.
    Escape Sequence: Escape sequenceis used in the printf() function to do something to the output. Escape Sequence Effect a Beep sound, alarm b Backspace f Form feed (for printing) n New line r Carriage return (move the curser to beginning of current line) t Tab v Vertical tab Backslash (to print at output) ” Used to print (“) sign ’ Used to print (‘) sign 27
  • 25.
    Format Specifier Tells theprintf() function the format of the output to be printed put No Format Specifier Output Type Output Example 1 %d or %i Signed decimal integer (short) 76 2 %u Unsigned decimal integer (short) 76 3 %o Unsigned octal integer 134 4 %x Unsigned hexadecimal (small letter) 9c 5 %X Unsigned hexadecimal (capital letter) 9C 6 %f Integer including decimal point 76.0000 7 %e Signed floating point (using e notation) 7.6000e+01 8 %E Signed floating point (using E notation) 7.6000E+01 9 %g The shorter between %f and %e 76 10 %G The shorter between %f and %E 76 11 %c Single Character (both for signed & unsigned) ‘7’ 12 %s String “76” 28
  • 26.
    Filed–Width Specifiers: With thehelp of this Interpretation of a variable’s type Width of the field Number of decimal places printed Justification 29
  • 27.
    Example # 1:Field-Width Specifer: A g e i s 3 3 . A g e i s 3 3 . A g e i s 3 3 . printf(“Age is %2d.”, age); printf(“Age is %4d.”, age); printf(“Age is %6d.”, age); 30
  • 28.
    Example # 2:Field-Width Specifer: main() { Float temp=27.25; printf(“The temperature is %f.”, temp); } Output: The Temperature is 27.250000 main() { Float temp=27.25; printf(“The temperature is %.2f.”, temp); } Output: The Temperature is 27.25 31
  • 29.
    Study the followingexample: main() { printf(“%.1f%8.1f%8.1fn”,3.0,12.5,523.3); printf(“%.1f%8.1f%8.1fn”,300.0,1200.5,52300.3); } What will be the output?? 32
  • 30.
    Output of PreviousExample:  output 3.0 12.5 523.3 300.0 1200.5 52300.3 In other way 3 . 0 1 2 . 5 5 2 3 . 3 3 0 0 . 0 1 2 0 0 . 5 5 2 3 0 0 . 3 8 8 8 8 33
  • 31.
    Study the followingexample: main() { printf(“%-.1f%-8.1f%-8.1fn”,3.0,12.5,523.3); printf(“%-.1f%-8.1f%-8.1fn”,300.0,1200.5,52300.3); } What will be the output?? 34
  • 32.
    Output of PreviousExample:  output In other way 3 . 0 1 2 . 5 5 2 3 . 3 3 0 0 . 0 1 2 0 0 . 5 5 2 3 0 0 . 3 8 8 8 8 300.0 1200.5 52300.3 3.0 12.5 523.3 35
  • 33.
    Explanation: %8.1f % 8 . 1 f Signalsformat specification field width # of digits to the right of decimal Indicates decimal-format floating point 36
  • 34.
    Scanf and Assignment: scanf: Itis used to save the function input into its relavent variable. Assignment operator: Its takes answer of a statement and saves into other variable 37
  • 35.
    Hierarchy of Operation: Priorityoperator Description 1st * / % multiplication, division , modular division 2nd + - addition , subtraction 3rd = Assignment 38
  • 36.
    Associativity of operators When two expression contains two operators of equal priority the tie between them is settled using the associativity of the operators . 1. Left to right (left operand must be unambiguous) 2. Right to left (right operand must be unambiguous) Means it must not be involved in evaluation of any other sub-expression Example: a=3/2*5 39
  • 37.
    Example: a=3/2*5 Operator leftright remark / 3 2 or 2*5 Left operand is unambiguous , Right is not * 3/2 or 2 5 Right operand is unambiguous , Left is not Meanings: Since both have / and * have L to R associativity and Only / has unambiguous, left operand (necessary condition for L to R associativity is performed earlier. 40
  • 38.
  • 39.
    Evaluation: kk=3/2*4+3/8+3 Solution:  kk=3/2*4+3/8+3  kk=1*4+3/8+3operation : /  kk=4+3/8+3 operation : *  kk=4+0+3 operation : /  kk=4+3 operation : +  kk=7 operation : + 45
  • 40.
  • 41.
    Evaluation Exercise:  i=2*3/4+4/4+8-2+5/8 Solution: i=2*3/4+4/4+8-2+5/8  i=6/4+4/4+8-2+5/8 operation : *  i=1+4/4+8-2+5/8 operation : /  i=1+1+8-2+5/8 operation : /  i=1+1+8-2+0 operation : /  i=2+8-2+0 operation : +  i=10-2+0 operation : +  i=8+0 operation : -  i=8 operation : + 47
  • 42.