0% found this document useful (0 votes)
4 views27 pages

CHP - 3 - Operators and Precedence

The document provides an overview of operators in the C programming language, including arithmetic, relational, logical, bitwise, and assignment operators, along with examples of their usage. It also discusses operator precedence and how it affects expression evaluation. Additionally, it includes lab exercises for practical application of the concepts covered.

Uploaded by

elyesaposlu31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

CHP - 3 - Operators and Precedence

The document provides an overview of operators in the C programming language, including arithmetic, relational, logical, bitwise, and assignment operators, along with examples of their usage. It also discusses operator precedence and how it affects expression evaluation. Additionally, it includes lab exercises for practical application of the concepts covered.

Uploaded by

elyesaposlu31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAMMING I

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:

• Relational operators are used together with a conditional flow


control keyword in C language which is if statement.
if Statement
• An if statement consists of a boolean (logical) expression
followed by one or more statements.
Syntax:
• The syntax of an if statement in C programming language is:

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

• C programming language assumes any non-zero and non-null


values as true and if it is either zero or null then it is assumed as
false value.
Relational Operators
• The following example show the relational operators’ use:
#include <stdio.h>
void main()
{
int a = 21;
int b = 10;
int c ;
printf(" a=%i and b=%i\n",a,b );

if( a == b ) printf(" = opr - a is equal to b\n");


if( a != b ) printf(" a is not equal to b\n" );
if ( a < b ) printf(" < opr - a is less than b\n" );
if ( a > b ) printf(" > opr - a is greater than b\n" );
}
Logical Operators
• Following table shows all the logical operators supported by C
language.
• Assume variable A holds 1 and variable B holds 0, then:
Logical Operators
• Try the following example to understand all the logical
operators available in C programming language:
#include <stdio.h>
void main()
{
int a = 5;
int b = 20;
int c = 15;
if ( a < b && b > c) printf(" 1st AND opr - Condition is true\n");
if ( a > b && b > c ) printf(" 2nd AND opr - Condition is true\n");
if ( a > b || b > c ) printf(" OR opr - Condition is true\n");
if (! (a > b)) printf(" NOT opr - Condition is true\n");
}
Bitwise Operators
• Bitwise operator works on bits and performs bit-by-bit
operation.

• These operators are & (and) , | (or), ^ (exor) and ~


(complement).
• The truth tables for &, |, and ^ are as follows:
Bitwise Operators
• The Bitwise operators supported by C language are listed in the
following table.
• Assume variable A holds 60 and variable B holds 13, then:
Bitwise Operators
• 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 = 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

• Output of the program:


Operators Precedence in C

• 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.
Operators Precedence in C
• 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.
Operators Precedence in C
• 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.
Operators Precedence in C
• Try the following example to understand the operator
precedence available in C programming language:

#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

• Output of the program:


LAB Exercise
• Write a C program which prints a 3 digit number as characters to
the screen.
LAB Exercise 1
• Write a C program which converts a 3 digit number as characters
to 3 digit integer number and perform some aritmetical
operations.
• Number characters normally will be entered one by one but in our
case they will be given in character variables as ch1, ch2 and ch3.

#include <stdio.h>
void main()
{
char ch1 = '2';
char ch2 = '8';
char ch3 = '4';

}
LAB Exercise 2

• Write a C program which prints a 3 digit number input from


keyboard as characters to the screen.

You might also like