Rajarata University of Sri Lanka: ICT1402 Principles of Program Design & Programming
Rajarata University of Sri Lanka: ICT1402 Principles of Program Design & Programming
ICT1402
PRINCIPLES OF PROGRAM DESIGN &
PROGRAMMING
Lecture 08
C Operators
K.A.S.H. Kulathilake
Ph.D., M. Phil., MCS, B.Sc. (Hons) IT, SEDA(UK)
2
Objectives
• At the end of this lecture students should be able
to;
▫ Define the terms operators, operands, operator
precedence and associativity.
▫ Describe operators in C programming language.
▫ Practice the effect of different operators in C
programming language.
▫ Justify evaluation of expressions in programming.
▫ Apply taught concepts for writing programs.
3
Arithmetic Operators
Operator Description
A+B adds A with B;
A-B subtracts B from A;
A*B multiplies A by B;
A/B divides A by B;
I%J gives the remainder of I divided by J.
Here I and J are expressions of any integer data type;
Logical Operators
Operator Description
•A
A && B has the value 1 if both A and B are nonzero, and 0
otherwise (and B is evaluated only if A is nonzero);
A || B has the value 1 if either A or B is nonzero, and 0
otherwise (and B is evaluated only if A is zero);
!A has the value 1 if a is zero, and 0 otherwise.
int main()
{
int a = 5;
int b = 20;
int c = 1;
Relational Operators
Operator Description
A<B has the value 1 if A is less than B, and 0 otherwise;
A <= B has the value 1 if A is less than or equal to B, and 0
otherwise;
A>B has the value 1 if A is greater than B, and 0 otherwise;
A >= B has the value 1 if A is greater than or equal to B, and 0
otherwise;
A == B has the value 1 if A is equal to B, and 0 otherwise;
A != B has the value 1 if A is not equal to B, and 0 otherwise;
int main()
{
int a = 5;
int b = 20;
int c = 5;
Bitwise Operators
Operator Description
A&B performs a bitwise AND of A and B;
A|B performs a bitwise OR of A and B;
A^B performs a bitwise XOR of A and B;
~A takes the bitwise complement of A;
A << n shifts A to the left n bits;
A >> n shifts A to the right n bits;
int main()
{
int a = 60;
int b = 13;
}
21
int main() {
int a = 21;
int c =10;;
c = a++;
printf( "Line 1 - Value of a and c are : %d - %d\n", a, c);
c = ++a;
printf( "Line 2 - Value of a and c are : %d - %d\n", a, c);
c = a--;
printf( "Line 3 - Value of a and c are : %d - %d\n", a, c);
c = --a;
printf( "Line 4 - Value of a and c are : %d - %d\n", a, c);
return 0;
}
23
Assignment Operators
• A = B stores the value of B into A;
• A op= B applies op to A and B, storing the
result in A.
int main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
28
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
return 0;
}
30
Conditional Operator
• Given that A, B, C are expressions; then the
expression
A?B:C
has as its value B if A is nonzero, and C otherwise;
only expression B or C is evaluated.
int main()
{
int num;
int flag;
flag = ((num%2==0)?1:0);
• Refere Lecture 04
34
sizeof Operator
• Given that type is as the name of a basic data
type, an enumerated data type (preceded by the
keyword enum), a typedef-defined type, or is a
derived data type; A is an expression; then the
expression
sizeof (type) has as its value the number of
bytes needed to contain a value of the specified
type;
sizeof A has as its value the number of bytes
required to hold the result of the evaluation of A.
35
int main() {
printf("%d\n", sizeof(ivar));
printf("%d\n", sizeof(cvar));
printf("%d\n", sizeof(fvar));
printf("%d\n", sizeof(dvar));
return 0;
}
36
Comma Operator
• The comma operator can be used to link the related
expressions together.
• A comma linked expression is evaluated from left to right
and the value of the right most expression is the value of
the combined expression.
x = (a = 2, b = 4, a+b)
• In this example, the expression is evaluated from left to
right.
• So at first, variable ‘a’ is assigned value 2, then variable
‘b’ is assigned value 4 and then value 6 is assigned to the
variable ‘x’.
• Comma operators are commonly used in for loops, while
loops, while exchanging values.
37
x = (a = 2, b = 4, a+b);
x = (a = 2, b = 4, a+b);
printf("%d\n", x);
return 0;
}
39
C Arithmetic Expression
• Arithmetic expression in C is a combination of
variables, constants and operators written in a
proper syntax.
• C can easily handle any complex mathematical
expressions but these mathematical expressions
have to be written in a proper syntax.
40
Summary of C Operators
• These operators are listed in order of decreasing
precedence.
• Operators grouped together have the same
precedence.
45
Questions
• Try this out:
▫ 250*2/4+1
▫ 5%2*5/5
▫ 2+300-200/2
▫ 1 && 1 || 0
▫ 1 || 1 +3-4 && 1
47
Objective Re-cap
• Now you should be able to:
▫ Define the terms operators, operands, operator
precedence and associativity.
▫ Describe operators in C programming language.
▫ Practice the effect of different operators in C
programming language.
▫ Justify evaluation of expressions in programming.
▫ Apply taught concepts for writing programs.
48
References
• Chapter 04, Appendix A, section 5 -
Programming in C, 3rd Edition, Stephen G.
Kochan
49