C – Operators
and Expressions
 The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
 These C operators join individual constants and variables to form expressions.
 Operators, functions, constants and variables are combined together to form expressions.
 Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.
Types of C operators:
C language offers many types of operators. They
are,
Types of Operators

Description

Arithmetic_operators  These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus 
Assignment_operators  These are used to assign the values for the variables in C programs. 
Relational operators  These operators are used to compare the value of two variables. 
Logical operators  These operators are used to perform logical operations on the given two variables. 
Bit wise operators  These operators are used to perform bit operations on given two variables. 
Conditional (ternary) operators  Conditional operators return one value if condition is true and returns another value is condition is false. 
Increment/decrement operators  These operators are used to either increase or decrease the value of the variable by one. 
Special operators  &, *, sizeof( ) and ternary operators. 
Arithmetic Operators in C:
C Arithmetic operators are used to perform
mathematical calculations like addition, subtraction, multiplication,
division and modulus in C programs.
Arithmetic Operators/Operation

Example

+ (Addition)

A+B

– (Subtraction)

AB

* (multiplication)

A*B

/ (Division)

A/B

% (Modulus)

A%B

Assignment operators in C:
 In C programs, values for the variables are assigned using assignment operators.
 For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
 There are 2 categories of assignment operators in C language. They are,
1.Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example:+=, =, *=, /=, %=, &=, ^= )
Operators

Example/Description

=

sum = 10; 10 is assigned to variable sum 
+=

sum += 10; This is same as sum = sum + 10 
=

sum = 10; This is same as sum = sum – 10 
*=

sum *= 10; This is same as sum = sum * 10 
/=

sum /= 10; This is same as sum = sum / 10 
%=

sum %= 10; This is same as sum = sum % 10 
&=

sum&=10; This is same as sum = sum & 10 
^=

sum ^= 10; This is same as sum = sum ^ 10 
Relational operators in C:
Relational operators are used to find the relation
between two variables. i.e. to compare the values of two variables in
a C program.
Operators

Example/Description

>

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) 
==

x == y (x is equal to y) 
!=

x != y (x is not equal to y) 
Logical operators in C:
 These operators are used to perform logical operations on the given expressions.
 There are 3 logical operators in C language. They are, logical AND (&&), logical OR () and logical NOT (!).
Operators

Example/Description

&& (logical AND) 
(x>5)&&(y<5)
It returns true when both conditions are true 
 (logical OR) 
(x>=10)(y>=10)
It returns true when atleast one of the condition
is true

! (logical NOT) 
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5)
&& (y<5))”
If “((x>5) && (y<5))” is true,
logical NOT operator makes it false

Bit wise operators in C:
 These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
Below are the bitwise operators in C language.
 & – Bitwise AND
  – Bitwise OR
 ~ – Bitwise NOT
 ^ – XOR
 << – Left Shift
 >> – Right Shift
Truth table for bit wise operation&Bit wise operators:
Conditional or ternary operators in C:
 Conditional operators return one value if condition is true and returns another value is condition is false.
 This operator is also called as ternary operator.
Syntax
: (Condition? true_value: false_value);
Example
: (A > 100 ? 0 : 1);
 In above example, if A is greater than 100, 0 is returned else 1 is returned.This is equal to if else conditional statements.
Increment/decrement
operators in C:
 Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
 Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – var_name; (or) var_name – ;  Example:
Increment operator :++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
Special Operators in C:
Below are some of the special operators that the C
programming language offers.
Operators

Description

&

This is used to get the address of the
variable.
Example : &a will give address of a.

*

This is used as pointer to a variable.
Example : * a where, * is pointer to the
variable a.

sizeof ()

This gives the size of the variable.
Example : sizeof (char) will give us 1.

Operator Precedence:
Operator precedence determines the grouping of
terms in an expression and decides how an expression is evaluated.
Certain operators have higher precedence than others; for example,
the multiplication operator has a higher precedence than the addition
operator.
For example, x = 7 + 3 * 2; here, x is assigned
13, not 20 because operator * has a higher precedence than +, so it
first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear
at the top of the table, those with the lowest appear at the bottom.
Within an expression, higher precedence operators will be evaluated
first.
Precedence  Operator  Description  Associativity 

1  ++   Suffix/postfix increment and decrement  Lefttoright 
()  Function call  
[]  Array subscripting  
.  Structure and union member access  
>  Structure and union member access through pointer  
(type){list}  Compound literal(C99)  
2  ++   Prefix increment and decrement  Righttoleft 
+   Unary plus and minus  
! ~  Logical NOT and bitwise NOT  
(type)  Type cast  
*  Indirection (dereference)  
&  Addressof  
sizeof  Sizeof^{[note 1]}  
_Alignof  Alignment requirement(C11)  
3  * / %  Multiplication, division, and remainder  Lefttoright 
4  +   Addition and subtraction  
5  << >>  Bitwise left shift and right shift  
6  < <=  For relational operators < and ≤ respectively  
> >=  For relational operators > and ≥ respectively  
7  == !=  For relational = and ≠ respectively  
8  &  Bitwise AND  
9  ^  Bitwise XOR (exclusive or)  
10    Bitwise OR (inclusive or)  
11  &&  Logical AND  
12    Logical OR  
13  ?:  Ternary conditional  RighttoLeft 
14  =  Simple assignment  
+= =  Assignment by sum and difference  
*= /= %=  Assignment by product, quotient, and remainder  
<<= >>=  Assignment by bitwise left shift and right shift  
&= ^= =  Assignment by bitwise AND, XOR, and OR  
15  ,  Comma  Lefttoright 
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is :%d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is :%d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is :%d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is :%d\n" , e );
return 0;
}
Comments
Post a Comment