際際滷

際際滷Share a Scribd company logo
Basic c operators
Basic c operators
Arithmetic operators
1. Unary operators
2. Binary operators
 Assignment operators
 Equalities and Relational operators
 Logical operators
 Conditional operators


In C , we have the following operators.
operation

operator

initialization

Addition

+

a+ b = c

Subtraction

-

a-b=c

Multiplication

*

a*b=c

Division

/

a/b=c

Increment

++

a++

Decrement

__

a--

Modulus

%

a%b=c


There are two types of arithmetic operators in
C:
1. Unary operators:


Operators that require only one operand

2. Binary operators:


Operators that require two operands.
C operation

operator

Example

Positive

+

a = +3

Negative

-

b = -a

Increment

++

i++

decrement

--

i--

The first assigns the positive value of 3 to a.
The second assigns the negative value of a
i++ is equivalent to i = i + 1.
i-- is equivalent to i = i - 1.

to b.





It is also possible to use ++i and -i instead
of i++ and i
However , the two forms have a slightly yet
important difference.
Consider following example:
 int a = 9;
 Printf(%d n , a++);
 Printf(%d , a);






The output would be:
9
10


But if we have:
int a=9;
printf(%d n , ++a);
printf(%d , a);



The output would be :
10
10





a++ would return the current value of a and
then increment the value of a
++a on the other hand increment the value of
a before returning the value
++or-Statement

Equivalent statements

r value

Count
value

r = count++;

r = count;
count = count + 1;

10

11

r = ++count;

count = count + 1;
r = count;

11

11

r = count--;

r = count;
count = count - 1;

10

9

r = --count;

count = count - 1;
r = count;

9

9
C operation

operator

Example

Addition

+

a+3

Subtraction

-

a6

Multiplication

*

a*b

Division

/

a/b

modulus

%

a%x

 The division of variables of type int will always produce a variable
of type int as the result.



You could only use modulus (%) operation on int variables.




Assignment operators are used to combine
the = operator with one of the binary
arithmetic operators.
in following, c=9

operator

example

Equivalent statement

Result

+=

c += 7

c=c+7

c = 16

-=

c -= 8

c=c8

c=1

*=

c *= 10

c = c * 10

c = 90

/=

c /= 5

c=c/5

c=1

%=

c %= 5

c=c%5

c=4





This comes into play when there is a mixed of
arithmetic operators in one statement.
Eg. x = 3*a - ++b % 3;
The rules specify which of the operators will
be evaluated first.
Precedence level

operator

Associativity

1(highest)

()

Left to right

2

Unary

Right to left

3

*/%

Left to right

4

+-

Left to right

5(lowest)

= += -= *= /= %=

Right to left


Equality operators:
operator

Meaning

==

x == y

x is equal to y

!=



example
x != y

x is not equal to y

Relational operators:
operator

example

Meaning

>

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






Logical operators are useful when we want to
test multiple conditions.
There are 3 types of logical operators and they
work the same way as the boolean AND,OR,NOT
operators.
&& - Logical AND
 All the conditions must be true for whole expression
to be true.
 Eg. if(a==10 && b==9 && d==1) means if
statement is true only if a=10,b=9,d=1.


|| - Logical OR
 The truth of one condition is enough to make
the whole expression true.
 Eg. if(a == 10 || b==9 || d==1) means
expression is true either a , b or d has right
value.



! - Logical NOT

 Reverse the meaning of a condition.

 Eg. if(!(a>90)) means if a is bigger than 90





The conditional operator (?:) is used to
simplify an if/else statement.
Condition ? Expression 1 : expression 2
The statement above is equivalent to:

if(condition)
expression 1
else
expression 2


This presentation exposed you the operators
used in C










Arithmetic operators
Assignment operators
Equalities & relational operators
Logical operators
Conditional operators

Precedence levels come into play when there
is mixed arithmetic operators in one
statement.
Pre/pro fix-effects the result of statement.
Basic c operators

More Related Content

Basic c operators

  • 3. Arithmetic operators 1. Unary operators 2. Binary operators Assignment operators Equalities and Relational operators Logical operators Conditional operators
  • 4. In C , we have the following operators. operation operator initialization Addition + a+ b = c Subtraction - a-b=c Multiplication * a*b=c Division / a/b=c Increment ++ a++ Decrement __ a-- Modulus % a%b=c
  • 5. There are two types of arithmetic operators in C: 1. Unary operators: Operators that require only one operand 2. Binary operators: Operators that require two operands.
  • 6. C operation operator Example Positive + a = +3 Negative - b = -a Increment ++ i++ decrement -- i-- The first assigns the positive value of 3 to a. The second assigns the negative value of a i++ is equivalent to i = i + 1. i-- is equivalent to i = i - 1. to b.
  • 7. It is also possible to use ++i and -i instead of i++ and i However , the two forms have a slightly yet important difference. Consider following example: int a = 9; Printf(%d n , a++); Printf(%d , a); The output would be: 9 10
  • 8. But if we have: int a=9; printf(%d n , ++a); printf(%d , a); The output would be : 10 10 a++ would return the current value of a and then increment the value of a ++a on the other hand increment the value of a before returning the value
  • 9. ++or-Statement Equivalent statements r value Count value r = count++; r = count; count = count + 1; 10 11 r = ++count; count = count + 1; r = count; 11 11 r = count--; r = count; count = count - 1; 10 9 r = --count; count = count - 1; r = count; 9 9
  • 10. C operation operator Example Addition + a+3 Subtraction - a6 Multiplication * a*b Division / a/b modulus % a%x The division of variables of type int will always produce a variable of type int as the result. You could only use modulus (%) operation on int variables.
  • 11. Assignment operators are used to combine the = operator with one of the binary arithmetic operators. in following, c=9 operator example Equivalent statement Result += c += 7 c=c+7 c = 16 -= c -= 8 c=c8 c=1 *= c *= 10 c = c * 10 c = 90 /= c /= 5 c=c/5 c=1 %= c %= 5 c=c%5 c=4
  • 12. This comes into play when there is a mixed of arithmetic operators in one statement. Eg. x = 3*a - ++b % 3; The rules specify which of the operators will be evaluated first. Precedence level operator Associativity 1(highest) () Left to right 2 Unary Right to left 3 */% Left to right 4 +- Left to right 5(lowest) = += -= *= /= %= Right to left
  • 13. Equality operators: operator Meaning == x == y x is equal to y != example x != y x is not equal to y Relational operators: operator example Meaning > 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
  • 14. Logical operators are useful when we want to test multiple conditions. There are 3 types of logical operators and they work the same way as the boolean AND,OR,NOT operators. && - Logical AND All the conditions must be true for whole expression to be true. Eg. if(a==10 && b==9 && d==1) means if statement is true only if a=10,b=9,d=1.
  • 15. || - Logical OR The truth of one condition is enough to make the whole expression true. Eg. if(a == 10 || b==9 || d==1) means expression is true either a , b or d has right value. ! - Logical NOT Reverse the meaning of a condition. Eg. if(!(a>90)) means if a is bigger than 90
  • 16. The conditional operator (?:) is used to simplify an if/else statement. Condition ? Expression 1 : expression 2 The statement above is equivalent to: if(condition) expression 1 else expression 2
  • 17. This presentation exposed you the operators used in C Arithmetic operators Assignment operators Equalities & relational operators Logical operators Conditional operators Precedence levels come into play when there is mixed arithmetic operators in one statement. Pre/pro fix-effects the result of statement.