Operators in C
Operators in C
What is a C Operator?
An operator in C can be defined as the symbol that helps us to perform some
specific mathematical, relational, bitwise, conditional, or logical computations
on values and variables. The values and variables used with operators are
called operands. So we can say that the operators are the symbols that
perform operations on operands.
Unary Operators
In C programming, unary operators are operators that operate on a single
operand. These operators are used to perform operations such as negation,
incrementing or decrementing a variable, or checking the size of a variable.
They provide a way to modify or manipulate the value of a single variable in
an efficient manner.
C provides 9 unary operators that can be used to perform various operations
on a single variable. These include:
Table of Content
Unary Plus
Unary Minus
Increment
Decrement
Logical NOT ( ! )
Bitwise NOT ( ! )
Addressof Operator ( & )
Indirection Operator (*)
sizeof()
1. Increment Operator (++)
The increment operator ( ++ ) is used to increment the value of the variable
by 1. The increment can be done in two ways:
A. Prefix Increment
In this method, the operator precedes the operand (e.g., ++a). The value of
the operand will be altered before it is used. For example:
int a = 1;
int b = ++a; // b = 2
B. Postfix Increment
In this method, the operator follows the operand (e.g., a++). The value
operand will be altered after it is used. For example:
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
Below example shows the implementation of increment ( ++ ):
#include <stdio.h>
int main(){
int a = 1;
int b = 1;
printf("Pre-Incrementing a = %d\n", ++a);
printf("Post-Incrementing b = %d", b++);
return 0;
}
Output
Pre-Incrementing a = 6
Post-Incrementing b = 5
int main() {
int a = 1;
int b = 1;
printf("Pre-Decrementing a = %d\n", --a);
printf("Post-Decrementing b = %d", b--);
return 0;
}
Output
Pre-Decrementing a = 4
Post-Decrementing b = 5
Unary Plus
The unary plus (+) operator does not change the sign of its argument; it
simply returns the value as is. It is often used for code clarity rather than
functionality.
int a = -10;
int b = +a; // b = -10
The unary plus is different from the addition operator, as addition requires
two operands.
Below is the implementation of the unary plus (+) operator:
#include <stdio.h>
int main() {
Output
-100
-100
Unary Minus
The minus operator ( – ) changes the sign of its argument. A positive
number becomes negative, and a negative number becomes positive.
int a = 10;
int b = -a; // b = -10
Unary minus is different from the subtraction operator, as subtraction
requires two operands.
Below is the implementation of the unary minus (-) operator:
#include <stdio.h>
int main(){
Output
100
-100
Logical NOT ( ! )
The logical NOT operator ( ! ) is used to reverse the logical state of its
operand. If a condition is true, then the Logical NOT operator will make it
false.
Example:
If x is true, then !x is false
If x is false, then !x is true
Below is the implementation of the NOT (!) operator:
#include <stdio.h>
int main(){
int a = 10;
int b = 5;
return 0;
}
Output
a is greater than b
Bitwise NOT ( ~ )
The bitwise NOT (~) operator inverts all bits of its operand. Each 0 becomes
1, and each 1 becomes 0. It effectively calculates the two’s complement
negative equivalent of a number in signed integers.
x = 5 (00000101 in binary),
~x = ~6 (11111010 in binary, two’s complement representation)
Below is the implementation of the bitwise NOT (~) operator:
#include <stdio.h>
int main() {
// Declaring an integer
int x = 5;
Output
x = 5
~x = -6
int a = 20;
printf("%p", &a);
return 0;
}
Output
0x7fffb23a4e9c
Output
20
sizeof()
This operator returns the size of its operand, in bytes.
The sizeof() operator always precedes its operand. The operand is an
expression, or it may be a cast.
Note: The `sizeof()` operator in C++ is machine dependent. For example,
the size of an ‘int’ in C++ may be 4 bytes in a 32-bit machine but it may be 8
bytes in a 64-bit machine.
Below is the implementation of sizeof() operator:
#include <stdio.h>
int main(){
return 0;
}
Output
8
4
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and
variables).
Operator Meaning of Operator
* multiplication
/ division
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0.
== Equal to 5 == 3 is evaluated to 0
return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false. Logical operators are
commonly used in decision making in C programming.
Operator Meaning Example
Logical AND. True only if all operands If c = 5 and d = 2 then, expression ((c==5) &&
&&
are true (d>5)) equals to 0.
Operator Meaning Example
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
||
operand is true (d>5)) equals to 1.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
sss
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Binary Operator in C:
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
// Logical OR (||)
printf("(a > 0) || (b > 0) = %d\n", (a > 0) || (b > 0));
// Equal to (==)
printf("a == b = %d\n", a == b);
return 0;
}