18
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

Types of operators in C

Embed Size (px)

DESCRIPTION

Types of operators in C in S-Teacher

Citation preview

Page 1: 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

Page 2: Types of operators in C

Arithmetic operatorsOperator Meaning

+ Addition or Unary Plus

– Subtraction or Unary Minus

* Multiplication

/ Division

% Modulus Operator

Page 3: Types of operators in C

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

Page 4: Types of operators in C

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

Page 5: Types of operators in C

Mixed mode arithmetic

15/10.0 = 1.5

Page 6: Types of operators in C

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

Page 7: Types of operators in C

Relational Expressions

6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE

Page 8: Types of operators in C

Logical Operators

Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT

Page 9: Types of operators in C

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

Page 10: Types of operators in C

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

Page 11: Types of operators in C

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

Page 12: Types of operators in C

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

Page 13: Types of operators in C

Conditional or Ternary Operator

a = 10; b = 15; x = (a > b) ? a : b

Page 14: Types of operators in C

Bitwise Operators Operator Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive

<< Shift left

>> Shift right

Page 15: Types of operators in C

Special Operators

comma operatorsize of operatorpointer operators (& and *) member selection operators (. and ->).

Page 16: Types of operators in C

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;

Page 17: Types of operators in C

The size of Operator

m = sizeof (sum); n = sizeof (long int); k = sizeof (235L);

Page 18: Types of operators in C

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 ); }