ALL SIR NOTES HAVE BEEN UPLOADED HERE.
An operator is a symbol that operates on a value or a variable. An operator tells the computer to perform certain mathematical or logical manipulations. For example: + is an operator to perform addition.
Operators are:
Unary Operators
Binary Operators
Ternary Operator(Conditional Operator)
An expression is a sequence of operands and operators that reduce to a single value. When both the operands in a single arithmetic expression are integers, the expression is called an integer expression.
Integer arithmetic always yields an integer value. An arithmetic operation involving only real operands is called real arithmetic expression.
When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression and the result is always a real number.
C operators can be classified into a number of categories. They include:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
C provides all the basic arithmetic operators. The operators +, -, *, and / all work the same way as they do in other languages. The % operator is known as modulus operator. It produces the remainder after the division of two operands.
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division
Relational operators is used to compare two operands. While the operands can be variables, constants or expressions, the result is always either true or false.
Operator Meaning
== is equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
More than one relational expression can be combined to form a single compound relational expression using logical operators. A compound relation behaves identically producing either true or false. C provide three logical operators:
Logical AND (&&) Logical OR (||) Logical NOT (!)
ex1 ex2 ex1&&ex2 ex1 ex2 ex1||ex2 exp !exp
0 0 0 0 0 0 0 1
1 0 0 1 0 1 1 0
0 1 0 0 1 1
1 1 1 1 1 1
Assignment operators are used to store the result of an expression to a variable. Note that data type of both the sides should be either the same or compatible to each other.
x += 10; is equivalent to x = x + 10;
x -= 10; is equivalent to x = x - 10;
x *= 10; is equivalent to x = x * 10;
x /= 10; is equivalent to x = x / 10;
x %= 10; is equivalent to x = x % 10;
These are unary operators as they require only one operand. When they are used before the operand, it is termed as prefix, while when used after the operand, they are termed as postfix operators. In expressions, postfix operator uses the current value and then increments/decrements while in the prefix form the value is incremented/ decremented first and then used in the expression.
For example,
b = a++; is equivalent to
b = a;
a = a + 1;
and b = -- a;
is equivalent to
a = a - 1;
b = a;
This is the only ternary type operator in C. This operator is a set of two operators (? and :) which work together. Conditional operator always works with relational operators.
Syntax:
condition ? exp1 : exp2;
If condition is true then value returned will be exp1, otherwise the value returned will be exp2.
Bitwise operators are used for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not be applied to float or double. A list of different bitwise operators are:
Bitwise Logical Operators
(i) Bitwise Logical AND (&)
(ii) Bitwise Logical OR (|)
(iii) Bitwise Logical XOR (^)
Bitwise Shift Operators
(i) Bitwise Left shift (<<)
(ii) Bitwise Right shift (>>)
Bitwise One’s complement (~)
The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis.
op1 op2 op1&op2
0 0 0
1 0 0
0 1 0
1 1 1
The truth table for the OR operator is:
op1 op2 op1|op2
0 0 0
1 0 1
0 1 1
1 1 1
The truth table for the XOR operator is:
op1 op2 op1^op2
0 0 0
1 0 1
1 0 1
1 1 0
It shifts each bit in its left operand to the right. Thus, ch>>2 would shift all bits in ch two places to the right. Similarly, ch>>3 would shift all bits in ch 3 places to the right. The blanks created on the left are always filled with zeros.
Example:
int n = 16;
n = n>>2; //now n will be 4
In left shift the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number.
Example:
int n = 4;
n = n<<2; //now n will be 16
Also referred as the complementation operator. It is a unary operator that inverts the bits of the operand, i.e. all 0 become 1 and all 1 become o. The operand for the operator must always be an integer value.
C language also provides number of special operators which have no counter parts in other languages.
comma operator
sizeof operator
pointer operators (& and *)
member selection operators (. and ->)
This operator is generally used in the for loop. The expression separated by comma operator are solved from left to right.
The sizeof operator works on variables, constants and even on all the data types. It returns the number of bytes the operand occupies in the memory.
Typecasting is basically a process in C in which we change a variable belonging to one data type to another one. The process of type casting can be performed in two major types in a C program. These are:
Implicit Type conversion
Explicit Type Conversion
C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion. During evaluation, if the operands are of different types, the ‘lower’ type is automatically converted to the ‘higher’ type before the operation proceeds.
There are instances when we want to force a type conversion in a way that is different from the automatic conversion. The process of such a local conversion is known as explicit conversion or casting a value. Example:
int x = 10, y = 4;
float z;
z = x/y;
z = (float) x / (float) y;
Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence. Precedence defines the sequence in which operators are to be applied on the operands, while evaluating the expressions involving more than one operators. The operators at the higher level of precedence are evaluated first. Operators of same precedence are evaluated from left to right or right to left, depending upon the level. This is known as associativity property of an operator.
Precedence rules decides the order in which different operators are applied.
Associativity rule decides the order in which multiple occurrences of the same level operator are applied