Operators in C Programming

Submitted by Karthikeyan on

Types of Operators in C Programming

Arithmetic Operators

(five)

+ - Addition / unary plus

- - Subraction / unary minus

* - Mulitplication

/ - Division

% - Modulo division

  • % - can’t be used with floating point data
  • C does not have operator for exponentiation
  • In modulo divison, the sign of the result is always the sign of the first operand

e.g.

 -14%3 = -2

14%-3 = 2

Relational Operators

(six)

<  is less than (is complement of >=)

<=  is less than or equal to

>  is greater than (is complement of <=)

>= is greater than or equal to

== is equal to (is complement of !=)

!= is not equal to

Logical Operators

&& - AND

|| - OR

! – NOT

Combines two or more relational expressions, is termed as a logical expression

Assignment Operators

var = var op (exp)

è var op= var

(var – variable, op – operator, exp – expression)

e.g.

 

Simple Assignment Operators

Equivalent Shorthand assignment operators

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

 

Increment and Decrement Operators

++ (Increment)

-- (Decrement)

  • Unary operators
  • Postfix : expression evaluated first, then the value of the variable is incremented / decremented by 1.
  • Prefix: the value of the variable is incremented / decremented by 1, then the expression evaluated.

e.g.

++a, a++

--b, b--

Expression

Value after processing

a = 5;

b = ++a;

a = 6

b = 6

a = 5;

b = a++;

a = 6

b = 5

 

Conditional Operator

Ternary operator pair  “?:”

exp1 ? exp2 : exp3

exp1 is evaluated first, if non zero(true), then exp2 is evaluated, exp1 is false, then exp3 is evaluated.

if(a>b)

x = a;

else

x = b; è

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

 

Bitwise Operator

 Used to manipulate the data at bit level.

& - bitwise AND

| - bitwise OR

^ - bitwise exclusive OR
<< - shift left

>> - shift right

  • Bitwise operators may not be applied to float or double

Special Operators

Comma (,) operator, sizeof operator, pointer operators (& and *), member selection operator (.  and ->)

Comma Operator:

  • Can be used to link the related expressions together
  • Comma op has the lowest precedence of all operators

e.g. in for loops
for(n=1, m=10, n<=m; n++, m++)

size of Operator:

  • It is compile time operator and it returns number of bytes the operand occupies.

e.g.

m = sizeof(sum);

n = sizeof(long int);

k = sizeof(255L);