UNIT 2
Operator
An operator is a symbol that operates on a value or a variable. For example: + is
an operator to perform addition.
C has a wide range of operators to perform various operations.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and
variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ Division
% remainder after division (modulo division)
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language.
Operator Description Example
+ Adds two operands. A+B=
30
− Subtracts second operand from the first. A−B=
-10
* Multiplies both operands. A*B=
200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer B%A=
division. 0
++ Increment operator increases the integer value by one. A++ =
11
-- Decrement operator decreases the integer value by A-- = 9
one.
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
Operator Description Example
== Checks if the values of two operands are equal or not. (A == B)
If yes, then the condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. (A != B)
If the values are not equal, then the condition is true.
becomes true.
> Checks if the value of left operand is greater than the (A > B)
value of right operand. If yes, then the condition is not
becomes true. true.
< Checks if the value of left operand is less than the (A < B)
value of right operand. If yes, then the condition is true.
becomes true.
>= Checks if the value of left operand is greater than or (A >= B)
equal to the value of right operand. If yes, then the is not
condition becomes true. true.
<= Checks if the value of left operand is less than or (A <= B)
equal to the value of right operand. If yes, then the is true.
condition becomes true.
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0, then −
Examples
Operator Description Example
&& Called Logical AND operator. If both the operands (A &&
are non-zero, then the condition becomes true. B) is
false.
|| Called Logical OR Operator. If any of the two (A || B)
operands is non-zero, then the condition becomes is true.
true.
! Called Logical NOT Operator. It is used to reverse !(A &&
the logical state of its operand. If a condition is true, B) is
then Logical NOT operator will make it false. true.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Operator Description Example
& Binary AND Operator copies a bit to the result (A & B) = 12,
if it exists in both operands. i.e., 0000 1100
| Binary OR Operator copies a bit if it exists in (A | B) = 61,
either operand. i.e., 0011 1101
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49,
in one operand but not both. i.e., 0011 0001
<< Binary Left Shift Operator. The left operands
A << 2 = 240
value is moved left by the number of bits
i.e., 1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands
A >> 2 = 15
value is moved right by the number of bits
i.e., 0000 1111
specified by the right operand.
Assignment Operators
The following table lists the assignment operators supported by the C language
−
Examples
Operator Description Example
= Simple assignment operator. Assigns values from C=A+
right side operands to left side operand B will
assign the
value of
A + B to
C
+= Add AND assignment operator. It adds the right C += A is
operand to the left operand and assign the result to equivalent
the left operand. to C = C
+A
-= Subtract AND assignment operator. It subtracts the C -= A is
right operand from the left operand and assigns the equivalent
result to the left operand. to C = C -
A
*= Multiply AND assignment operator. It multiplies the C *= A is
right operand with the left operand and assigns the equivalent
result to the left operand. to C = C
*A
/= Divide AND assignment operator. It divides the left C /= A is
operand with the right operand and assigns the result equivalent
to the left operand. to C = C /
A
%= Modulus AND assignment operator. It takes C %= A
modulus using two operands and assigns the result to is
the left operand. equivalent
to C = C
%A
Operator Precedence and Associativity in C
Operator precedence determines which operator is performed first in an
expression with more than one operators with different precedence.
10 + 20 * 30
Operators Associativity is used when two operators of same precedence
appear in an expression. Associativity can be either Left to Right
or Right to Left.
For example: ‘*’ and ‘/’ have same precedence and their associativity
is Left to Right, so the expression “100 / 10 * 10” is treated as
“(100 / 10) * 10”.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Preprocessor Directive
Preprocessor directives are lines included in a program that begin with the
character #, which make them different from a typical source code text. They
are invoked by the compiler to process some programs before compilation.
Preprocessor directives change the text of the source code and the result is a
new source code without these directives.
#define and #undef
#if, #elif, #else
Comments
Comments provide clarity to the C source code allowing others to better
understand what the code was intended to accomplish and greatly helping in
debugging the code. Comments are especially important in large projects
containing hundreds or thousands of lines of source code or in projects in which
many contributors are working on the source code.
A comment starts with a slash asterisk /* and ends with a asterisk slash */ and
can be anywhere in your program. Comments can span several lines within your
C program. Comments are typically added directly above the related C source
code.
// C program to demo
// Single Line comment
#include <stdio.h>
int main(void)
{
// This is a single line comment
printf("iimt");
return 0; // return zero
}