0% found this document useful (0 votes)
22 views13 pages

Operators in C

The document provides an overview of operators in the C programming language, detailing unary and binary operators, their definitions, and examples of usage. It covers various types of operators including arithmetic, relational, logical, and bitwise operators, explaining their functions and providing code snippets for illustration. Additionally, it highlights the significance of operators in performing operations on operands within C programming.

Uploaded by

eshanlodi36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

Operators in C

The document provides an overview of operators in the C programming language, detailing unary and binary operators, their definitions, and examples of usage. It covers various types of operators including arithmetic, relational, logical, and bitwise operators, explaining their functions and providing code snippets for illustration. Additionally, it highlights the significance of operators in performing operations on operands within C programming.

Uploaded by

eshanlodi36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Operators in C

In C language, operators are symbols that represent operations to be


performed on one or more operands. They are the basic components of the
C programming. In this article, we will learn about all the built-in operators in
C with examples.

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

2. Decrement Operator (–)


The decrement operator ( — ) is used to decrement the value of the
variable by 1. The decrement can be done in two ways:
A. Prefix Decrement
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 = 0
B. Postfix Decrement
In this method, the operator follows the operand (e.g., a- -). The value of the
operand will be altered after it is used. For example:
int a = 1;
int b = a–; // b = 1
int c = a; // c = 0
Below example shows the implementation of decrement (–):
#include <stdio.h>

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() {

// Declaring a negative integer


int a = -10;

// Using unary plus to keep value unchanged


int b = +a;
printf("%d\n", a);
printf("%d", b);
return 0;
}

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(){

// declaring a positive integer


int a = 10;

// using - sign to make the value of positive integers


// to negative
int b = -a;
printf("%d\n", a);
printf("%d", b);
return 0;
}

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;

if (!(a > b))


printf("b is greater than a\n");
else
printf("a is greater than b");

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;

// Applying bitwise NOT


int res = ~x;

printf("x = %d\n", x);


printf("~x = %d\n", res);
return 0;
}

Output
x = 5
~x = -6

Addressof Operator (&)


The addressof operator ( & ) gives an address of a variable. It is used to
return the memory address of a variable. These addresses returned by the
address-of operator are known as pointers because they “point” to the
variable in memory.
Example:
& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.
Below is the implementation of the Addressof operator(&):
#include <stdio.h>
int main(){

int a = 20;
printf("%p", &a);
return 0;
}

Output
0x7fffb23a4e9c

Indirection Operator (*)


The indirection operator (*), also known as the dereference operator, is
used to access the value stored at a memory address. It is used with
pointers to retrieve the value stored at the referenced memory location.
a = 20;
ptr = &a;
*ptr // This will give 20
Below is the implementation of indirection operator:
#include <stdio.h>
int main(){
int a = 20;
int *ptr = &a;
printf("%d", *ptr);
return 0;
}

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(){

// printing the size of double and int using sizeof


printf("%d\n", sizeof(double));
printf("%d", sizeof(int));

return 0;
}
Output
8
4

Binary Operators in Programming

Binary Operators are essential tools in programming that perform


operations on pairs of data, enabling tasks like calculations, comparisons,
logical operations, and bitwise manipulations. They are fundamental for
processing and manipulating data efficiently in computer programs.

What are Binary Operators?

Binary Operators are operators in programming that perform operations


on two operands. These operands can be variables, constants, or
expressions. Binary operators are called binary because they operate on
two operands.

Basics of Binary Operators:

Binary operators, as the name suggests, operate on two operands and


perform various computations or comparisons. These operators are integral
to arithmetic, bitwise operations, and relational evaluations within
programming languages. Here, we'll discuss common binary operators and
their applications.

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

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo division)

Example 1: Arithmetic Operators


// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, 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("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.

Relational operators are used in decision making and loops.


Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

Example : Relational Operators


// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

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.

Logical NOT. True only if the operand is


! If c = 5 then, expression !(c==5) equals to 0.
0

Example : Logical Operators


// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", 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:

Here are the implementation of Binary Operator in C language:

#include <stdio.h>

int main()
{
int a = 10;
int b = 5;

// Binary addition (+)


printf("a + b = %d\n", a + b);

// Binary subtraction (-)


printf("a - b = %d\n", a - b);

// Binary multiplication (*)


printf("a * b = %d\n", a * b);

// Binary division (/)


printf("a / b = %d\n", a / b);

// Binary modulo (%)


printf("a % b = %d\n", a % b);

// Binary bitwise AND (&)


printf("a & b = %d\n", a & b);

// Binary bitwise OR (|)


printf("a | b = %d\n", a | b);

// Binary bitwise XOR (^)


printf("a ^ b = %d\n", a ^ b);

// Binary left shift (<<)


printf("a << 1 = %d\n", a << 1);

// Binary right shift (>>)


printf("a >> 1 = %d\n", a >> 1);

// Logical AND (&&)


printf("(a > 0) && (b > 0) = %d\n", (a > 0) && (b > 0));

// Logical OR (||)
printf("(a > 0) || (b > 0) = %d\n", (a > 0) || (b > 0));

// Equal to (==)
printf("a == b = %d\n", a == b);

// Not equal to (!=)


printf("a != b = %d\n", a != b);

// Greater than (>)


printf("a > b = %d\n", a > b);

// Less than (<)


printf("a < b = %d\n", a < b);

// Greater than or equal to (>=)


printf("a >= b = %d\n", a >= b);

// Less than or equal to (<=)


printf("a <= b = %d\n", a <= b);

return 0;
}

You might also like