Types of operators in C
-
Upload
prabhu-govind -
Category
Education
-
view
258 -
download
1
description
Transcript of Types of operators in C
C Operators
1. Arithmetic operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increments and Decrement Operators 6. Conditional Operators 7. Bitwise Operators 8. Special Operators
Arithmetic operatorsOperator Meaning
+ Addition or Unary Plus
– Subtraction or Unary Minus
* Multiplication
/ Division
% Modulus Operator
Integer Arithmetic
Let x = 27 and y = 5 z = x + y 32
z = x – y 22 z = x * y 115 z = x % y 2 z = x / y 5
Floating point arithmetic
Let x = 14.0 and y = 4.0 then z = x + y 18.0 z = x – y 10.0 z = x * y 56.0 z = x / y 3.50
Mixed mode arithmetic
15/10.0 = 1.5
Relational OperatorsOperator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Relational Expressions
6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Find out the answer
a > b && x = = 10 a < m || a < n! (x >= y)
a = 5b = 4
x=10, y = 9 m=6 n = 3
Assignment Operators Statement with simple
assignment operatorStatement with shorthand operator
a = a + 1 a += 1
a = a – 1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a = a % b a %= b
What is the output?
#define N 100 //creates a variable N with constant value 100 #define A 2 //creates a variable A with constant value 2 main() //start of the program {
int a; //variable a declaration a = A; //assigns value 2 to a
while (a < N) //while value of a is less than N { //evaluate or do the following
printf(“%d \n”,a); //print the current value of a a *= a; //shorthand form of a = a * a
} //end of the loop } //end of the program
Output
2 4
16
Increment and Decrement Operators
1. ++ v2. v++ 3. – –v4. V--
m = 5; y = ++m; (prefix) After Execution:
y=6, m=6
m = 5; y = m++; (post fix)After Execution:
y=5, m=6
Conditional or Ternary Operator
a = 10; b = 15; x = (a > b) ? a : b
Bitwise Operators Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive
<< Shift left
>> Shift right
Special Operators
comma operatorsize of operatorpointer operators (& and *) member selection operators (. and ->).
The Comma Operator
value = (x = 10, y = 5, x + y);
In for loops: for (n=1, m=10, n <=m; n++,m++)
In while loops While (c=getchar(), c != ‘1’)
Exchanging values
t = x, x = y, y = t;
The size of Operator
m = sizeof (sum); n = sizeof (long int); k = sizeof (235L);
What is the output?
main() //start of program { int a, b, c, d; //declaration of variables a = 15; b = 10; c = ++a-b; //assign values to variables printf (“a = %d, b = %d, c = %d\n”, a,b,c); //print the values d=b++ + a; printf (“a = %d, b = %d, d = %d\n, a,b,d); printf (“a / b = %d\n, a / b); printf (“a %% b = %d\n, a % b); printf (“a *= b = %d\n, a *= b); printf (“%d\n, (c > d) ? 1 : 0 ); printf (“%d\n, (c < d) ? 1 : 0 ); }