0% found this document useful (0 votes)
3 views19 pages

Programming LU3

The document provides an overview of operators in the C programming language, defining them as symbols that perform operations on variables and values. It classifies operators into categories such as arithmetic, relational, logical, bitwise, and assignment, and includes examples of each type. Additionally, it discusses operator precedence and associativity, explaining how they affect the evaluation of expressions.

Uploaded by

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

Programming LU3

The document provides an overview of operators in the C programming language, defining them as symbols that perform operations on variables and values. It classifies operators into categories such as arithmetic, relational, logical, bitwise, and assignment, and includes examples of each type. Additionally, it discusses operator precedence and associativity, explaining how they affect the evaluation of expressions.

Uploaded by

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

Operators

BICT1201
Operators Definition
Operators are used to perform operations on variables and values.

 Operators are symbols that represent some kind of operation to be performed.


 They are the basic components of the C programming language.
 The symbol helps the programmer perform some specific mathematical, relational,
bitwise, conditional, or logical computations on values and variables.
 The values and variables used with operators are called operands.
Classification of Operators
Operators in C are classified according to their functionality. The classification of
the operators is as follows
 Arithmetic
 Relational
 Logical
 Bitwise
 Assignment
Arithmetic Operators
The arithmetic operators are used to perform mathematical operations on operands.

The arithmetic operators include


+ plus
- minus
* multiplication
/ division
% modulus
+ unary plus
- unary minus
 ++ increment
 -- decrement
Arithmetic Operators Example
#include <stdio.h> printf("a % b = %d\n", a % b);

printf("+a = %d\n", +a);


int main() {

printf("-a = %d\n", -a);


int a = 25, b = 5;
printf("a++ = %d\n", a++);

// using operators and printing results printf("a-- = %d\n", a--);


printf("a + b = %d\n", a + b);

printf("a - b = %d\n", a - b);


return 0;
printf("a * b = %d\n", a * b);

printf("a / b = %d\n", a / b); }


Relational Operators
 Relational operators are used for the comparison of two operands. They are
binary operators that return true or false values from contrast.
The relational operators include
< less than
> greater than
 <= less than or equal to
 >= greater than or equal to
 == equal to
 != not equal to
Relational Operators
#include <stdio.h> printf("a <= b: %d\n", a <= b);

printf("a >= b: %d\n", a >= b);


int main() { printf("a == b: %d\n", a == b);
int a = 20, b = 2;
printf("a != b : %d\n", a != b);

// using operators and printing results


return 0;
printf("a < b : %d\n", a < b);
}
printf("a > b : %d\n", a > b);
Logical Operators
Logical Operators combine two or more conditions to complement the evaluation
of the original condition in consideration.
The logical operators include
 && - Logical AND, returns true if both operands are true
 || - Logical OR, returns true if either or both of the operands are true
 ! – Logical NOT, returns true if the operand is false
Logical Operators Example
#include <stdio.h> printf("a || b : %d\n", a || b);

printf("!a: %d\n", !a);


int main() {

int a = 25, b = 5;
return 0;
// using operators and printing
}
results

printf("a && b : %d\n", a && b);


Bitwise Operators
Bitwise operators are used to perform bit-level operations on the operands.
 The operators are first converted to bit-level, and then the calculation is performed on
the operands. The symbols include;

& Bitwise AND


| Bitwise OR
^ Bitwise XOR
~ Bitwise First Complement
 << Bitwise Leftshift
 >> Bitwise Rightshift
Bitwise Operators Example
#include <stdio.h> printf("a ^ b: %d\n", a ^ b);

printf("~a: %d\n", ~a);


int main() { printf("a >> b: %d\n", a >> b);
int a = 25, b = 5;
printf("a << b: %d\n", a << b);

// using operators and printing results


return 0;
printf("a & b: %d\n", a & b);
}
printf("a | b: %d\n", a | b);
Assignment Operators
Assignment operators are used to assign a value to a variable. The left side
operand of the assignment operator is a variable, and the right side operand is a
value.
The symbols include
= simple assignment
 += plus and assign
 -= minus and assign
 *= multiply and assign
 /= divide and assign
 %= modulus and assign
 &= AND and assign
Assignment Operators
 |= OR and assign
 ^= XOR and assign
 >>= Rightshift and assign
 <<= Leftshift and assign
Assignment Operators Example
#include <stdio.h> printf("a /= b: %d\n", a /= b);

printf("a %%= b: %d\n", a %= b);

int main() { printf("a &= b: %d\n", a &= b);


int a = 25, b = 5; printf("a |= b: %d\n", a |= b);

printf("a ^= b: %d\n", a ^= b);


// using operators and printing results printf("a >>= b: %d\n", a >>= b);
printf("a = b: %d\n", a = b); printf("a <<= b: %d\n", a <<= b);
printf("a += b: %d\n", a += b);

printf("a -= b: %d\n", a -= b); return 0;


printf("a *= b: %d\n", a *= b); }
Operator Precedence &
Associativity
 Operator precedence determines the order in which operators are evaluated in
expressions.
 Operators with higher precedence are evaluated first.

Associativity determines the direction (left to right or right to left) in which


operators with the same precedence are evaluated.
The following slide tabulates the order of precedence and associativity in C.
Operator Precedence &
Associativity
#include <stdio.h> int e;

e = a + b * c / d;
int main(){
printf("e : %d\n" , e );

int a = 20;
return 0;
int b = 10;

int c = 15; }

int d = 5;
Operator Precedence &
Associativity
#include <stdio.h>

int main() {

int a = 10, b = 5, c = 2;

int result;
// Expression to evaluate

result = a + b * c - a / b;

printf("Result: %d\n", result);

return 0;

}
Operator Precedence &
Associativity
#include <stdio.h> printf("a: %d\n", a); // a is 5

printf("b: %d\n", b); // b is 3


int main() {
printf("c: %d\n", c); // c is 5
int a = 3;

int b = a++; // b = 3, a becomes 4,


postfix return 0;
int c = ++a; // a becomes 5, c = 5,
prefix
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy