0% found this document useful (0 votes)
24 views

Operators in C

Uploaded by

rajesh93601615
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)
24 views

Operators in C

Uploaded by

rajesh93601615
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/ 7

Operators in C

 o 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.

o 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.
Types of Operators in C
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Increment And Decrement Operators
7. Size of Operators
8. Conditional Operators

1. Arithmetic Operations
The arithmetic operators are used to perform arithmetic/mathematical
operations on operands.

2. Relational Operators in C
The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or false
values as the result of comparison.
3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or
to complement the evaluation of the original condition in consideration. The
result of the operation of a logical operator is a Boolean value
either true or false.
Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations such as
addition, subtraction, multiplication, etc. can be performed at the bit level for
faster processing.
Example:

// C program to illustrate the bitwise operators


#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);

return 0;
}
Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
5. Shorthand Assignment Operators:
Assignment operators are used to assign value to a variable. The left side
operand of the assignment operator is a variable and the right side operand
of the assignment operator is a value.
Example:

#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


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

return 0;
}

Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
)a >>= b: 0
a <<= b: 160
sizeof Operator:
 sizeof is much used in the C programming language.
 It is a compile-time unary operator which can be used to compute the size
of its operand.
 The result of sizeof is of the unsigned integral type which is usually
denoted by size_t.
 Basically, the sizeof the operator is used to compute the size of the
variable or datatype.

Syntax
sizeof (operand)

Conditional Operator ( ? : )
 The conditional operator is the only ternary operator in C++.
 Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the result
of Expression2 otherwise if the condition(Expression1) is false then we
will execute and return the result of Expression3.
 We may replace the use of if..else statements with conditional operators.
Syntax
operand1 ? operand2 : operand3;

Increment And Decrement Operator:

The increment ( ++ ) and decrement ( — ) operators in C are unary operators for


incrementing and decrementing the numeric values by 1 respectively.

Increment Operator in C:

The increment operator ( ++ ) is used to increment the value of a variable


in an expression by 1. It can be used on variables of the numeric type such
as integer, float, character, pointers, etc.
Types:

1. Pre-Increment
2. Post-Increment
1. Pre-Increment
In pre-increment, the increment operator is used as the prefix. Also known as
prefix increment, the value is incremented first according to the precedence
and then the less priority operations are done.
Example
result = ++var1;
2. Post-Increment
In post-increment, the increment operator is used as the suffix of the
operand. The increment operation is performed after all the other operations
are done. It is also known as postfix increment.
Example
result = var1++;

Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an
expression. In the Pre-Decrement, the value is first decremented and then
used inside the expression. Whereas in the Post-Decrement, the value is
first used inside the expression and then decremented.

Types:

1. Pre-Decrement Operator
2. Post-Decrement Operator

1. Pre-Decrement Operator
The pre-decrement operator decreases the value of the variable immediately
when encountered. It is also known as prefix decrement as the decrement
operator is used as the prefix of the operand.
Example
result = --m;

2. Post-Decrement Operator
The post-decrement happens when the decrement operator is used as the
suffix of the variable. In this case, the decrement operation is performed after
all the other operators are evaluated.
Example
result = m--;

You might also like