Professional Documents
Culture Documents
TOPIC 03
o A binary operator
o Used in assigning value to an operand
Syntax:
Operand1 = Operand2;
o Left hand side operand (Operand1) should have (L-Value) such as variable
o Right hand side operand (Operand2) can be constant, another variable, expression or function
ASSIGNMENT OPERATORS
o Example:
x= 2; // constant
x= y; // other variable
x= 2 * y; // expression
x= sin (y); // function
o Type of the result will follow the left hand side operand
int x = 7/2; /*x value is 3 not 3.5*/
float y = 3; /*y value is 3.000 */
ASSIGNMENT OPERATORS
o Example:
x= 2; // constant
x= y; // another variable
x= 2 * y; // expression
x= sin (y); // function
+ Addition x = y + 6;
- Subtraction y = x – 5;
* Multiply y = y * 3;
/ Division z = x/y;
% Modulo A = 10 % 3;
++ Increment x++;
-- Decrement z--;
• Modulo
– Symbol : %
– Binary operator
– To find remainder of a division
– N % 2, can be used to find an odd or even number
N % 2 = 0 N is even
N % 2 = 1 N is odd
• Example :
o N++; // post increment
o ++N; // pre increment
o N--; // post decrement
o --N; // pre decrement
• If a stand-alone statement, N++; or ++N; equal to N=N+1;
• If a stand-alone statement, N--; or --N; equal to N=N-1;
ARITHMETIC OPERATORS
• Example :
#include <stdio.h>
int main ()
{
int x = 44; int y = 44;
++x;
printf(”x = %d\n”, x); /* result 45 */
y++;
printf(”y = %d\n”, y); /* result 45 */
}
ARITHMETIC OPERATORS
• If ++n and n++ a bounded statements (sub-expression), then both of them have a different
meaning
• ++n n add by 1, then continue to process its expression
• n++ process the expression initially, then add n by 1
int main () {
int x=44; int y = 44; int z;
z = ++x; /* z, x is 45 */
z = y++; /* z is 44 and y is 45 */
return(0);
}
ARITHMETIC OPERATORS
a = a – b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;
a=a^b; a ^= b;
ARITHMETIC OPERATORS
o Example :
Sample
Operator Explanation
expression
++ ++a Increment a by 1, then use the new value of a in the
expression in which a resides.
++ a++ Use the current value of a in the expression in which a
resides, then increment a by 1.
-- --b Decrement b by 1, then use the new value of b in the
expression in which b resides.
-- b-- Use the current value of b in the expression in which b
resides, then decrement b by 1.
INCREMENT AND DECREMENT
OPERATORS (CONT)
#include <stdio.h>
// function main begins program execution
int main(void) {
// demonstrate postincrement
int c = 5; // assign 5 to c
printf("%d\n", c); // print 5
printf("%d\n", c++); // print 5 then postincrement
printf("%d\n\n", c); // print 6
Output :
// demonstrate preincrement 5
c = 5; // assign 5 to c 5
printf("%d\n", c); // print 5 6
printf("%d\n", ++c); // preincrement then print 6
printf("%d\n", c); // print 6 5
} // end function main 6
6
PRECEDENCE AND ASSOCIATIVE
o Example:
#include<stdio.h>
int main()
{
int x=5,y=6;
if ( x == y) printf("%d equal %d\n",x,y);
if ( x != y) printf("%d not equal %d\n",x,y);
if ( x < y) printf("%d less than %d\n",x,y);
if ( x > y) printf("%d greater than %d\n",x,y);
if ( x <= y) printf("%d less or equal than %d\n",x,y);
if ( x >= y) printf("%d greater or equal than %d\n",x,y);
return(0);
}
int x;
x = (20 > 10); // x value 1
x = (20 == 10); // x value 0
CONDITIONAL EXPRESSIONS
o Example:
int main () {
int code, discount=0;
code=1;
discount = (code == 1) ? 30 : 10;
printf(” Item discount = %d \n”,discount);
return(0);
}
int main () {
int bil, abs;
bil = 50;
abs = (bil1 > 0) ? bil : - bil;
printf(“%d \n”,bil);
bil = - 50;
abs = (bil1 > 0) ? bil : - bil;
printf(“%d \n”,bil);
return(0);
}
LOGICAL OPERATORS
Example :
int x=5; int y=0;
x && y; // FALSE
(x > y) && (y>=0); // TRUE
LOGICAL OPERATORS
A B !A A && B A || B
True True False True True
| OR A | B;
^ XOR A ^ B;
~ Complement ~A;
Example:
int A=24; int B=35; int C;
C = A & B; // value C = 0
C = A | B; // value C = 59
Note:
A=24 binary: 011000
B=35 binary: 100011
Bit by bit AND operation resulting: 000000, in decimal: 0
Bit by bit OR operation resulting : 111011, in decimal: 59
BITWISE OPERATORS
XOR operation of two bits resulting 1 if both bit are differ, and will result 0 if both are analogous.
Example:
int A,B=45;
A = B^75; // Result A=102
Note:
Decimal 45 binary: 0101101
Decimal 75 binary: 1001011
bit by bit XOR will result in : 1100110 or 102 in decimal
BITWISE OPERATORS
To create a complement-1 value, then every bit with 0 value exchange to 1 and vice versa.
Example:
int A, B=0xC3;
A=~B; //value A=0x3C;
Note:
0xC3 binary: 1100 0011
complement-1 result:
0011 1100 in hexadecimal 3C In C, writing a hexadecimal should
start with 0x
BITWISE OPERATORS
o Example :
• int A, B=78;
• A = B >> 3; //value A=9
• A = B << 2; //value A=312
o Operators are the calculation operation process, such as integer added with integer. Operand is
the data used to manipulate or carry out operations.
o Assignment operators are given with symbols (=). The operand on the left of the operator must
be a variable (L-value). The operand on the right must be an expression (r-value).
o Arimatika operators are operators used for arithmetic processing such as addition, reduction,
multiplication, division, two numbers (round or broken), the remaining for 2 integers, addition
and reduction of values between variables.
o Relational operators are operators used to compare 2 similar values. If the comparison results
are correct, the return value will be 1 (true), if wrong then the return value will be 0 (false).
o Conditional expression is a conditional operation in which this operator is triadic (trinary
operator) which requires 3 operands. Conditional expression uses symbols (:.).
o Logic operators are operators relating to logic operations, such as: negation (denial),
conjunction (and), disjunction (or), nor (exclusive OR).
o Bitwise operators are operators who treat their operands as quantity consisting of bits.
o Precedence and associative (precedence and associative) indicate the level or level of the
operator. Operators with higher predecessions will be done first. Associative explains how to
work on an expression if there is an operator with the same level or level in the predecession.
ThankYOU...
REFERENCES
o Paul J. Deitel & Harvey. Deitel. (2022). C how to program. 09. Pearson Education. Hoboken.
ISBN:978-0-13-739839-3. Chapter 2, 3, 4, 7 and 10
o Manipulating Data with Operators: http://aelinik.free.fr/c/ch06.htm