Unit - 2 Operator: Sejal Patel Ass. Prof, PICA, PU
Unit - 2 Operator: Sejal Patel Ass. Prof, PICA, PU
SEJAL PATEL
Ass. Prof, PICA, PU
Definition
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical functions.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Conditional Operators
Special Operator
Increment and decrement operator
Arithmetic Operator
• An arithmetic operator performs mathematical operations
such as addition, subtraction and multiplication on numerical
values (constants and variables).
Example
#include <stdio.h>
main()
{ Output
int a = 21; Line 1 - Value of c is 31
int b = 10;
int c ; Line 2 - Value of c is 11
c = a + b; Line 3 - Value of c is 210
printf("Line 1 - Value of c is %d\n", c ); Line 4 - Value of c is 2
c = a - b; Line 5 - Value of c is 1
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
}
Relational Operator
• A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0.
• Relational operators are used in decision making and loops Example A= 10 B =20
Example
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
printf("Line 1 - a is equal to b\n" );
} else {
printf("Line 1 - a is not equal to b\n" );
/* Lets change value of a and b */
}
a = 5;
b = 20;
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
if ( a <= b ) {
} else {
printf("Line 4 - a is either less than or equal to
printf("Line 2 - a is not less than b\n" ); b\n" );
} }
if ( a > b ) { if ( b >= a ) {
printf("Line 3 - a is greater than b\n" ); printf("Line 5 - b is either greater than or
} else { equal to b\n" );
printf("Line 3 - a is not greater than b\n" ); }
}
}
Output
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
Logical Operator
• An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in
C programming.
• A= 1 and B = 0
Example
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
Output:
printf("Line 1 - Condition is true\n" );
}
Line 1 - Condition is true
if ( a || b ) {
printf("Line 2 - Condition is true\n" ); Line 2 - Condition is true
} Line 3 - Condition is not true
/* lets change the value of a and b */
a = 0; Line 4 - Condition is true
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}
}
• Line 1 - Condition is true
• Line 2 - Condition is true
• Line 3 - Condition is not true
• Line 4 - Condition is true
Assignment Operators
• An assignment operator is used for assigning a value
to a variable. The most common assignment
operator is =
Example
#include <stdio.h>
main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
}
Output
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Bitwise Operators
• Bitwise operator works on bits and perform bit-by-bit
operation. The truth tables for &, |, and ^ are as follows:
Bitwise Operators
• Bitwise operator works on bits and perform bit-by-bit
operation. The truth tables for &, |, and ^ are as follows:
• 78: 1001110
• 30: 11110
• 51: 110011
• 75:1001011
Bitwise Operators
Binary Left Shift Operator
• << (left shift) Takes two numbers, left shifts
the bits of the first operand, the second operand
decides the number of places to shift. Or in other
words left shifting an integer “x” with an integer
“y” (x<<y) is equivalent to multiplying x with
2^y (2 raise to power y).
Binary Right Shift Operator
• >> (right shift) Takes two numbers, right
shifts the bits of the first operand, the second
operand decides the number of places to
shift.Similarly right shifting (x>>y) is equivalent
to dividing x with 2^y.
•
Important Points :
• The left shift and right shift operators should not be
used for negative numbers. The result of is
undefined behavior if any of the operands is a
negative number. For example results of both -1 <<
1 and 1 << -1 is undefined.
• If the number is shifted more than the size of
integer, the behavior is undefined. For example, 1
<< 33 is undefined if integers are stored using 32
bits. See this for more details.
• The left-shift by 1 and right-shift by 1 are equivalent
to multiplication and division by 2 respectively.
As mentioned in point 1, it works only if numbers
are positive.
Conditional operators
• They are also called as Ternary Operator .
• They also called as ?: operator
• Ternary Operators takes on 3 Arguments
Conditional operators
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
• where
• expression1 is Condition
• expression2 is Statement Followed if Condition is True
• expression2 is Statement Followed if Condition is False
Working of the ? Operator:
Exp1 is evaluated first, if it is nonzero(1/true) then
the expression2 is evaluated and this becomes
the value of the expression,
If exp1 is false(0/zero) exp3 is evaluated and its
value becomes the value of the expression.
Ex: m=2;
n=3
r=(m>n) ? m : n;
Ex: m=2;
n=3
r=(m>n) ? m : n;
Same as if(m>n){
r=a;
}
else{
r=b;
}
• #include <stdio.h>
• #include<conio.h>
• void main()
• {
• int mark;
• printf("Enter mark: ");
• scanf("%d", &mark);
• printf(mark >= 40 ? "Passed" : "Failed");
• getch();
• }
Increment & Decrement Operators
C supports 2 useful operators namely
1. Increment ++
2. Decrement –-
Example:
++a or a++
--a or a--
Rules for ++ & -- operators
• 2) Expanded source code is sent to compiler which compiles the code and
converts it into assembly code.
• 3) The assembly code is sent to assembler which assembles the code and
converts it into object code. Now a simple.obj file is generated.
• 4) The object code is sent to linker which links it to the library such as
header files. Then it is converted into executable code. A simple.exe file is
generated.
• 5) The executable code is sent to loader which loads it into memory and
then it is executed. After execution, output is sent to console.
Difference between compiler and
interpreter
Header file
• A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.
• There are two types of header files: the files that the programmer writes and
the files that comes with your compiler.
• You request to use a header file in your program by including it with the C
preprocessing directive #include, like you have seen inclusion of stdio.h
header file, which comes along with your compiler.
• Including a header file is equal to copying the content of the header file but
we do not do it because it will be error-prone and it is not a good idea to
copy the content of a header file in the source files, especially if we have
multiple source files in a program.
• #include "file" This form is used for header files of your own
program.