- Arithmetic Operators
C provides all the basic arithmetic operators. Following are the arithmetic operators in C: -
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
-- Decrement
++ Increment
+ and – comes in to variants as they can be used as unary operators and binary operators both. In case of unary - acts as returning the negative value, which is used to represent negative values for example,
int b = 10;
int a = -b; // a will be -10 now.
+, -, *, / will act in usual behavior, returning addition, subtraction, multiplication and division (quotient) of two operands. % (modulus) returns the division (remainder) instead quotient.
For example:-
int main()
{
int a, b, c, d;
a=5;
b=2;
c=1;
d=2;
printf("a=%d b=%d c=%d d=%d \n\n",a,b,c,d);
printf("a+b = %d\n", a + b);
printf("a-b=%d\n", a - b);
printf("a*b = %d\n", a * b);
printf("a/b = %d\n", a / b);
printf("a%%b = %d\n\n", a % b);
printf("c+d = %d\n", c + d);
printf("c-d = %d\n", c - d);
printf("c*d = %d\n", c * d);
printf("c/d = %d\n", c / d);
printf("c%%d = %d\n", c % d);
return 0;
}
int x=5;
x = x + 1; // x will be 6 now onwards.
int y=5;
y++; // y will be 6 now onwards.
int a=10;
a--; // a will be 9 now onwards.
int b =10;
b = b – 1; // b will be 9 now onwards.
So, a++ is same as a = a + 1 and a-- is same as a = a – 1.
Both increment and decrement operators can either be prefix to operand or postfix to operand, in those case, there are called as,
a++; // post increment
++a; // pre increment
a--; // post decrement
--a; // pre decrement
- Relational Operators
In C language, we can define true and false outputs based on some conditions. Any value other than 0 (ZERO) in C is treated as true, whereas ZERO is false. Be careful, all positive and negative numbers other than 0 are true, only 0 is treated as false in C language.
Relational operators return 0 when they found a false result, and 1 if they found a true result based on their conditions. Following are the relational operators: -
> greater than
>= greater than or equal
< less than
<= less than or equal
== equal
!= not equal
We can check any two expressions with these relational operators. For example,
4 == 4; // return 1 as they match
3 != 6; // return 0 as condition is false.
4 <= 9; // return 1 as condition satisfies
int main()
{
int a,b,c,d;
a=5;
b=2;
c=5;
d=8;
printf("a=%d b=%d c=%d d=%d \n\n",a,b,c,d);
if(a > b) printf("a is greater than b.\n");
if(a >= d) printf("a is greater than or equal to d.\n");
if(a == c) printf("a is equal to c.\n");
if(a != d) printf("a is not equal to d.\n");
if(d <= c) printf("d is less than or equal to c.\n");
if(d > c) printf("d is greater than c.\n");
return 0;
}
- Logical Operators
In C, from values, operands will be evaluated to either 0 or 1. These are used to combine relational operations, for example,
(3 > 1) && (5 != 6) // combining two conditions. Evaluated as (1 && 1) = 1
(1 > 3) && (5 != 6) // combining two conditions. Evaluated as (0 && 1) = 0
(1 > 3) || (5 != 6) // combining two conditions. Evaluated as (0 || 1) = 1
Program:
int main()
{
int a,b,c,d;
a=5;
b=2;
c=5;
d=8;
printf("a=%d b=%d c=%d d=%d \n\n",a,b,c,d);
if((a > b) && (a == c)) printf("a is greater than b AND a is equal to c.\n");
if((a <= b) && (d >= b)) printf("a is less than b AND d is greater than b.\n");
if((a != b) && (a <= d)) printf("a is not equal to b AND a is less than or equal to d.\n");
return 0;
}
- Bitwise Operators
C has a distinction of supporting special operators know as bitwise operators for manipulation of data at bit level. Bitwise operators work on individual bits and bytes of operands. Bitwise operators works on char and int variables only and may not be applied to float or double.& AND
| OR
^ XOR
~ NOT
>> Right Shift
<< Left Shift
variable >> number of bit positions
The general form of the shift-left statement is
variable << number of bit positions
For example:
- Assignment Operators
Assignment operators are used to assign the result of an expression to a variable. In C, ( = ) equals sign is a assignment operator. It takes two operands. First on left side whose value will get the new value assigned (known as lvalue), and second on right side whose value will be assigned to lvalue (known as rvalue). For example:
int a, b, c, d;
a = b = c = d = 10; // it will assign 10 to all these variables.
- Special Operators
The & and * operators! ~ ++ -- sizeof Right to left
+ - Left to right
>> << Left to right
< <= > >= Left to right
== != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
?: Right to left
= += -+ *= /+ *= Right to left
Lowest , Left to right