Professional Documents
Culture Documents
Prepared By:
Asst. Prof. Jyotsnarani Tripathy
Introduction
• C language supports a rich set of built-in
operators. An operator is a symbol that tells
the compiler to perform a certain
mathematical or logical manipulation.
• Operators are used in programs to manipulate
data and variables.
Types
• C operators can be classified into following types:
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Special operators
Arithmetic operators
• C supports all the basic arithmetic operators. The
following table shows all the basic arithmetic
operators.
Operator Description
% remainder of division
Relational operators
• The following shows all relation operators supported by C.
• Operator Description
• == Check if two operand are equal
• != Check if two operand are not equal.
• > Check if operand on the left is greater than operand on the
right
• < Check operand on the left is smaller than right operand
• >= check left operand is greater than or equal to right operand
• <= Check if operand on left is smaller than or equal to right
operand
Logical operators
• C language supports following 3 logical
operators. Suppose a = 1 and b = 0,
Operator Description Example
|| Logical OR (a || b) is true
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
• Ternary Operator
• ? : Operator
• It is actually the if condition that we use in C language decision
making, but using conditional operator, we turn the if condition
statement into a short and simple operator.
• Syntax
• data_type x;
• data_type *pt;
• pt = &x
Dereference operator ("*") or Pointer
Operater
• The dereference operator or indirection
operator, noted by asterisk ("*"), is also a unary
operator in c languages that uses for pointer
variables. It operates on a pointer variable, and
returns l-value equivalent to the value at the
pointer address. This is called "dereferencing"
the pointer.
• data_type *pt;
Double Pointer operator ("**")
• Double Pointer is, that double pointer points
to another pointer variable address.
• data_type **pt;
sizeof operator
• sizeof returns the size of a variable or datatype
• sizeof (data_type)
Increment and decrement operators in C
• Let’s explore the prefix and postfix of increment and decrement operators in C
• ++i (Pre-increment): It will increment the value of i even before assigning it to the
variable i.
• i++ (Post-increment): The operator will return the variable value first (i.e, i value)
then only i value is incremented by 1.
• –i (Pre decrement): It will decrement the value of i even before assigning it to the
variable i.
• i– (Post decrement): The operator returns the variable value first (i.e., i value), then
only i value decremented by 1.
• /* Increment and Decrement Operators in C as Prefix and Postfix */
• #include<stdio.h>
int main()
• {
int x = 10,y = 20, a = 5, b= 4;
printf("---- PRE INCREMENT OPERATOR EXAMPLE---- \n");
• printf("Value of x : %d \n", x); //Original Value
• printf("Value of x : %d \n", ++x); // using Pre increment Operator
• printf("Value of x Incremented : %d \n", x); //Incremented value
printf("----POST INCREMENT OPERATOR EXAMPLE---- \n");
• printf("Value of y : %d \n", y); //Original Value
• printf("Value of y : %d \n", y++); // using Post increment Operator
• printf("Value of Incremented y : %d \n", y); //Incremented value
• c = a & b; /* 20 = 010100 */
• printf("AND - Value of c is %d\n", c );
• c = a | b; /* 21 = 010101 */
• printf("OR - Value of c is %d\n", c );
• c = a ^ b; /* 1 = 0001 */
• printf("Exclusive-OR - Value of c is %d\n", c );
• }
• Output:
• AND - Value of c is 20
• OR - Value of c is 21
• Exclusive-OR - Value of c is 1
Examples
• #include <stdio.h>
• int main() {
• int a = 20; /* 20 = 010100 */
• int c = 0;
• c = a << 2; /* 80 = 101000 */
• printf("Left shift - Value of c is %d\n", c );