KEMBAR78
Operators | PDF | Multiplication | Arithmetic
0% found this document useful (0 votes)
18 views20 pages

Operators

The document provides an overview of various operators in C programming, including arithmetic, conditional, and bitwise operators. It explains the functionality and usage of different types of operators such as arithmetic, increment/decrement, assignment, relational, and logical operators, along with examples. Additionally, it covers operator precedence and the importance of parentheses in expressions to ensure correct evaluation.

Uploaded by

hassan01754
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)
18 views20 pages

Operators

The document provides an overview of various operators in C programming, including arithmetic, conditional, and bitwise operators. It explains the functionality and usage of different types of operators such as arithmetic, increment/decrement, assignment, relational, and logical operators, along with examples. Additionally, it covers operator precedence and the importance of parentheses in expressions to ensure correct evaluation.

Uploaded by

hassan01754
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/ 20

C Programming Operators

C programming has various operators to perform tasks including


arithmetic, conditional and bitwise operations. You will learn about
various C operators and how to use them in this tutorial.
An operator is a symbol which operates on a value or a variable.

For example: + is an operator to perform addition.

C programming has wide range of operators to perform various


operations. For better understanding of operators, these operators can
be classified as:
Operators in C programming
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators
C Arithmetic Operators

An arithmetic operator performs mathematical operations such as


addition, subtraction and multiplication on numerical values
(constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division( modulo division)


Example #1: Arithmetic Operators
// C Program to demonstrate the working of arithmetic operators #include
<stdio.h>
int main()
{
int a = 9,b = 4, c; Output
c = a+b;
printf("a+b = %d \n",c); a+b = 13
c = a-b; a-b = 5
printf("a-b = %d \n",c); a*b = 36
c = a*b; a/b = 2
printf("a*b = %d \n",c); Remainder when a divided by b=1
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
The operators +, - and * computes addition, subtraction and
multiplication respectively as you might have expected.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the


program. It is because both variables a and b are integers. Hence,
the output is also an integer. The compiler neglects the term after
decimal point and shows answer 2 instead of 2.25.

The modulo operator % computes the remainder. When a = 9 is


divided by b = 4, the remainder is 1. The % operator can only be
used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2.
Then in C programming,

a/b = 2.5 // Because both operands are floating-point variables


a/d = 2.5 // Because one operand is floating-point variable
c/b = 2.5 // Because one operand is floating-point variable
c/d = 2 // Because both operands are integers

Increment and decrement operators

C programming has two operators increment ++ and decrement -- to


change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1.
These two operators are unary operators, meaning they only
operate on a single operand.
Example #2: Increment and Decrement Operators

// C Program to demonstrate the working of increment and decrement operators #include


<stdio.h>
int main()
{ Output
int a = 10, ++a = 11
b = 100; - -b = 99
float c = 10.5, ++c = 11.500000
d = 100.5; ++d = 99.500000
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
Here, the operators ++ and -- are used
printf("++c = %f \n", ++c); as prefix. These two operators can also
printf("--d = %f \n", --d); be used as postfix like a++ and a--. Visit
return 0; this page to learn more on
} how increment and decrement
operators work when used as postfix.
C Assignment Operators

An assignment operator is used for assigning a value to a variable.


The most common assignment operator is =

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Example #3: Assignment Operators
// C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c; Output
c = a;
printf("c = %d \n", c);
c=5
c += a; // c = c+a
printf("c = %d \n", c); c = 10
c -= a; // c = c-a c=5
printf("c = %d \n", c); c = 25
c *= a; // c = c*a c=5
printf("c = %d \n", c);
c=0
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
C Relational Operators

A relational operator checks the relationship between two


operands. If the relation is true, it returns 1; if the relation is false,
it returns value 0.
Relational operators are used in decision making and loops.
Meaning of Operator Example
Operator

== Equal to 5 == 3 returns 0

> Greater than 5 > 3 returns 1

< Less than 5 < 3 returns 0

!= Not equal to 5 != 3 returns 1

>= Greater than or equal to 5 >= 3 returns 1

<= Less than or equal to 5 <= 3 return 0


Decision Making: Equality and
Relational Operators
Standard algebraic C equality or Example of C Meaning of C
equality operator or relational condition condition
relational operator operator
Equality Operators
= == x == y x is equal to y
not = != x != y x is not equal to y
Relational Operators
> > x > y x is greater than y
< < x < y x is less than y
>= >= x >= y x is greater than or
equal to y
<= <= x <= y x is less than or
equal to y

12
// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main() Output
{
int a = 5, b = 5, c = 10; 5 == 5 = 1
printf("%d == %d = %d \n", a, b, a == b); // true 5 == 10 = 0
printf("%d == %d = %d \n", a, c, a == c); // false 5>5=0
printf("%d > %d = %d \n", a, b, a > b); //false 5 > 10 = 0
printf("%d > %d = %d \n", a, c, a > c); //false 5<5=0
printf("%d < %d = %d \n", a, b, a < b); //false 5 < 10 = 1
printf("%d < %d = %d \n", a, c, a < c); //true 5 != 5 = 0
printf("%d != %d = %d \n", a, b, a != b); //false 5 != 10 = 1
printf("%d != %d = %d \n", a, c, a != c); //true 5 >= 5 = 1
printf("%d >= %d = %d \n", a, b, a >= b); //true 5 >= 10 = 0
printf("%d >= %d = %d \n", a, c, a >= c); //false 5 <= 5 = 1
printf("%d <= %d = %d \n", a, b, a <= b); //true 5 <= 10 = 1
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
Opera
Meaning of Operator Example
tor
Logial AND. True only if all If c = 5 and d = 2 then, expression ((c == 5) &&
&&
operands are true (d > 5))equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c == 5) ||
||
operand is true (d > 5))equals to 1.
Logical NOT. True only if the
! If c = 5 then, expression ! (c == 5) equals to 0.
operand is 0
Arithmetic
• Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
• 7 / 5 evaluates to 1
– Modulus operator(%) returns the remainder
• 7 % 5 evaluates to 2
• Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3

15
Arithmetic
• Arithmetic operators:
C o p era tio n Arithm etic Alg eb ra ic C exp ressio n
o p era to r exp ressio n
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s

• Rules Operation(s)
Operator(s)of operator precedence:
Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If there
are several pairs of parentheses “on the same level” (i.e.,
not nested), they are evaluated left to right.
*, /, or % Multiplication,Divi Evaluated second. If there are several, they are
sion, Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.
16
Expressions and Evaluation
Expressions combine Values using Operators, according to precedence.

1 + 2 * 2  1 + 4  5
(1 + 2) * 2  3 * 2  6

Symbols are evaluated to their Values before being combined.


int x=1;
int y=2;
x + y * y  x + 2 * 2  x + 4  1 + 4  5

Comparison operators are used to compare values.


In C, 0 means “false”, and any other value means “true”.
int x=4;
(x < 5)  (4 < 5)  <true>
(x < 4)  (4 < 4)  0
((x < 5) || (x < 4))  (<true> || (x < 4))  <true>

Not evaluated because


17 first clause was true
Comparison and Mathematical
== equal to
Operators
The rules of precedence are clearly
< less than defined but often difficult to remember or
<= less than or equal
> greater than non-intuitive. When in doubt, add
>= greater than or equal parentheses to make it explicit. For oft-
!= not equal
&& logical and
confused cases, the compiler will give you
|| logical or a warning “Suggest parens around …” – do
! logical not it!
+ plus & bitwise and
- minus | bitwise or Beware division:
* mult ^ bitwise xor • If second argument is integer, the
/ divide ~ bitwise not
% modulo << shift left result will be integer (rounded):
>> shift right 5 / 10  0 whereas 5 / 10.0  0.5
• Division by 0 will cause a FPE

Don’t confuse & and &&..


1 & 2  0 whereas 1 && 2  <true>
18
Assignment Operators
x = y assign y to x x += y assign (x+y) to x
x++ post-increment x x -= y assign (x-y) to x
++x pre-increment x x *= y assign (x*y) to x
x-- post-decrement x x /= y assign (x/y) to x
--x pre-decrement x x %= y assign (x%y) to x

Note the difference between ++x and x++:

int x=5; int x=5;


int y; int y;
y = ++x; y = x++;
/* x == 6, y == 6 */ /* x == 6, y == 5 */

Don’t confuse = and ==! The compiler will warn “suggest parens”.

int x=5; int x=5;


if (x==6) /* false */ if (x=6) /* always true */
{ {
/* ... */ /* x is now 6 */
recommendation
} }
/* x is still 5 */ /* ... */

19

You might also like