CHP - 3 - Operators and Precedence
CHP - 3 - Operators and Precedence
CHAPTER 3.
Operators and Precedence
CENGİZ RİVA
Operators
• 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
• Misc 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:
Arithmetic Operators
• The following example shows all the arithmetic operators
available in C programming language:
#include <stdio.h>
void main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Add. opr 1 - Value of c is %d\n", c );
c = a - b;
printf("Sub. opr 2 - Value of c is %d\n", c );
c = a * b;
printf("Mul. opr 3 - Value of c is %d\n", c );
c = a / b;
printf("Div. opr 4 - Value of c is %d\n", c );
c = a % b;
printf("Rem. opr 5 - Value of c is %d\n", c );
c = ++a;
printf("Inc. opr 6 - Value of c is %d\n", c );
c = --a;
printf("Dec. opr 7 - Value of c is %d\n", c ); }
Arithmetic Operators
• When we execute:
Relational Operators
• Following table shows all the relational operators:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100 = 12
A|B = 0011 1101 = 61
A^B = 0011 0001 = 49
~A = 1100 0011 = -61
A << 2 = 1111 0000 = 240
A >> 2 = 0000 1111 = 15
Bitwise Operators
• The following example shows all the bitwise operators available in
C programming language:
#include <stdio.h>
void main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b;
printf(" and opr- Value of c is %d\n", c );
c = a | b;
printf(" or opr - Value of c is %d\n", c );
c = a ^ b;
printf(" exor opr- Value of c is %d\n", c );
c = ~a;
printf(" compl opr- Value of c is %d\n", c );
c = a << 2;
printf(" shl opr- Value of c is %d\n", c );
c = a >> 2;
printf(" shr opr- Value of c is %d\n", c );
}
Bitwise Operators
• Output of the program:
Assignment Operators
• There are following assignment operators supported by C
language:
Assignment Operators
• There are following assignment operators supported by C
language:
Assignment Operators
• The following example shows all the assignment operators
available in C programming language:
#include <stdio.h>
void main()
{
int a = 21;
int c ;
c = a;
printf(" a=%i and c=%i\n",a, c);
c += a;
printf(" += Operator Example, Value of c = %d\n", c );
c -= a;
printf(" -= Operator Example, Value of c = %d\n", c );
c *= a;
printf(" *= Operator Example, Value of c = %d\n", c );
c /= a;
printf(" /= Operator Example, Value of c = %d\n", c );
}
Assignment Operators
#include <stdio.h>
void main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d;
printf("Value of (a + b) * c / d is : %d\n", e );
e = a + b * c / d;
printf("Value of a + b * c / d is : %d\n" , e );
e = ((a + b) * c) / d;
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d);
printf("Value of (a + b) * (c / d) is : %d\n", e );
}
Operators Precedence in C
#include <stdio.h>
void main()
{
char ch1 = '2';
char ch2 = '8';
char ch3 = '4';
}
LAB Exercise 2