>= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands."> < >= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands.">
C Operators, Operands, Expressions & Statements
C Operators, Operands, Expressions & Statements
• Arithmetic
• Relational
• Logical
• Assignment
• Increment and Decrement
• Conditional
• Bitwise
• Special
ARITHMETIC OPERATORS:
C provides all the basic arithmetic operators, they are +, -, *, /, %.
Integer division truncates (shorten) any fractional part.
The modulo division produces the remainder of an integer
division.
Ex: a+b
a–b
a*b
a/b
a%b
Here a and b are variables and are known as operands.
Note: Modulo (%) cannot be used for floating and double data
types.
Assume variable A holds 10 and variable B holds 20 then
== Checks if the values of two operands are equal or not, if yes (A == B) is not
true.
then condition becomes true.
> Checks if the value of left operand is greater than the value (A > B) is not
of right operand, if yes then condition becomes true. true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not
the value of right operand, if yes then condition becomes true.
true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
Logical Operators:
Assume variable A holds 1 and variable B holds 0, then
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume variable A holds 60 (00111100) and variable B holds 13 (00001101),
then: Operator Description Example
& Binary AND Operator copies a bit to the result if it (A & B) will give 12,
exists in both operands.
which is 0000 1100
| Binary OR Operator copies a bit if it exists in either (A | B) will give 61,
operand.
which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49,
one operand but not both.
which is 0011 0001
~ Binary Ones Complement Operator is unary and has (~A ) will give -61, which
the effect of ‗flipping‘ bits.
is 1100 0011 in 2‘s
complement form.
<< Binary Left Shift Operator. The left operands value A << 2 will give 240
is moved left by the number of bits specified by the
which is 1111 0000
right operand.
>> Binary Right Shift Operator. The left operands A >> 2 will give 15
value is moved right by the number of bits specified
which is 0000 1111
by the right operand.
<< (left shift operator):
First operand<< second operand
1. When bits are shifted left then trailing positions are filled with zeroes.
Example:
3<<1
0000 0011<<1
0000 0110
=6 i.e. (3* 21)
>> (right shift operator):
First operand >> second operand
1. When bits are shifted left then leading positions are filled with zeroes.
Example:
3>>1
0000 0011>>1
0000 0001
=1 i.e. (3/ 21)
Assignment Operators:
In C programs, values for the variables are assigned using assignment operators.
There are following assignment operators supported by C language:
here, x is assigned 13, not 20 because operator * has higher precedence than
+, so it first gets multiplied with 3*2 and then adds into 7.
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
Sr.No. Operator Name of Operator Associativity Rank
1. () L→R 1
Function call
[]
Array subscript
2. R→L 2
+ Unary plus
- Unary minus
++ Increment
--
Decrement
&
Address-of
*
Deference
7. == Equal to L→R 7
!= Not equal to
8. & Bitwise AND L→R 8
/= quotient Assign
%=
modulus Assign sum
+=
Assign difference
-=
Assign bitwise AND
&=
|= Assign bitwise OR
^=
Assign bitwise XOR
<<=
>>= Assign left shift
Assign right shift
15. , Comma operator L→R 15
Example:
#include <stdio.h>
void main()
{
int a=3,b=2;
Answer: 1,2
a=a==b==0;
printf("%d,%d",a,b); }
You can convert values from one type to another explicitly using the cast
operator. There are two types of type casting in c languages that are
implicit conversions and Explicit Conversions.
New data type should be mentioned before the variable name or value in
brackets which to be typecast.
• Implicit Conversion
• Explicit Conversion
1. IMPLICIT CONVERSION
Implicit conversions do not require any operator for converted. They are
automatically performed when a value is copied to a compatible type in
the program.
#include<stdio.h> #include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”, p);
getch();
}
Output:-
{
int i=20;
short p;
clrscr();
p = (short) i; // Explicit conversion
printf(“Explicit value is %”, p);
getch();