Operator in C
Operator in C
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example-:
#include <stdio.h>
void main()
{
int a=3,b=7,k;
k=a+b;
printf("sum of two numbers is %d\n", k);
}
Relational operators
Relational operators are used to comparing two quantities or values.
Operator Description
== Is equal to
!= Is not equal to
Logical Operator
C provides three logical operators when we test more than one condition to make
decisions. These are: && (meaning logical AND), || (meaning logical OR) and !
(meaning logical NOT).
Operator Description
&& And operator. It performs logical conjunction of two expressions. (if both expressions
evaluate to True, result is True. If either expression evaluates to False, the result is
False)
Assignment operators
Assignment operators applied to assign the result of an expression to a variable. C
has a collection of shorthand assignment operators.
Operator Description
= Assign
Conditional Operator
C offers a ternary operator which is the conditional operator (?: in combination) to
construct conditional expressions.
Operator Description
?: Conditional Expression
Example-:
main()
{
int a=10;
(a%2==0)?printf(“%d is even”,a):printf(“%d is odd”,a);
}
Output-: 10 is even
Special Operator
C supports some special operators
Operator Description
* Pointer to a variable.
Example-:
#include <stdio.h>
void main()
{
int i=10;
printf("integer: %d\n", sizeof(i));
}
Output-: integer: 2
Bitwise Operator in C
The bitwise operators are the operators used to perform the operations on the data at
the bit-level. When we perform the bitwise operations, then it is also known as bit-
level programming.
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1
Increment and decrement operators can be used only with variables. They can't be
used with constants or expressions.
int x = 1, y = 1;
++x; // valid
y = ++x;
Here first, the current value of x is incremented by 1. The new value of x is then
assigned to y. Similarly, in the statement:
y = --x;
#include<stdio.h>
int main()
{
int x = 12, y = 1;
y = --x;
y = x--;
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x); // print the initial value of x
printf("Initial value of y = %d\n\n", y); // print the initial value of y
y = x++; // use the current value of x then increment it by 1
printf("After incrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
y = x--;
printf("After decrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
return 0;
}