0% found this document useful (0 votes)
14 views7 pages

Operator in C

The document provides an overview of operators in the C programming language, categorizing them into types such as arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators. It explains the functionality of each operator type with examples, including how to use increment and decrement operators in both prefix and postfix forms. Additionally, it includes truth tables for bitwise operations and demonstrates the use of various operators through sample code snippets.

Uploaded by

ferak53573
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)
14 views7 pages

Operator in C

The document provides an overview of operators in the C programming language, categorizing them into types such as arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators. It explains the functionality of each operator type with examples, including how to use increment and decrement operators in both prefix and postfix forms. Additionally, it includes truth tables for bitwise operations and demonstrates the use of various operators through sample code snippets.

Uploaded by

ferak53573
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

Operator in C

C operators are symbols that are used to perform mathematical or


logical manipulations. The C programming language is rich with built-in
operators.
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators
Arithmetic Operators
Arithmetic Operators are used to performing mathematical calculations
like addition (+), subtraction (-), multiplication (*), division (/) and
modulus (%).
Operator Description

+ 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

> Greater than

< Less than

>= Greater than or equal to

<= Less than or 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)

|| Or operator. It performs a logical disjunction on two expressions. (if either or both


expressions evaluate to True, the result is True)

! Not operator. It performs logical negation on an expression.

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

+= Increments then assign

-= Decrements then assign

*= Multiplies then assign

/= Divides then assign

%= Modulus then assign

<<= Left shift and assign

>>= Right shift and assign

&= Bitwise AND assign

^= Bitwise exclusive OR and assign

|= Bitwise inclusive OR and 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

sizeof() Returns the size of a memory location.

& Returns the address of a memory location.

* 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.

Operator Meaning of operator

& Bitwise AND operator

| Bitwise OR operator

^ Bitwise exclusive OR operator

~ One's complement operator (unary operator)

<< Left shift operator

>> Right shift operator

The truth table of the bitwise operators.


X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 1

Increment and Decrement Operators in C


C has two special unary operators called increment (++) and decrement (--)
operators. These operators increment and decrement value of a variable by 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

++5; // invalid - increment operator operating on a consta


nt value

++(x+y);// invalid - increment operating on an expression

Increment/Decrement operators are of two types:

1. Prefix increment/decrement operator.


2. Postfix increment/decrement operator.

Prefix increment/decrement operator


The prefix increment/decrement operator immediately increases or decreases the
current value of the variable. This value is then used in the expression. Let's take an
example:

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;

the current value of x is decremented by 1. The new value of x is then assigned


to y.

The following program demonstrates prefix increment/decrement operator in action:

#include<stdio.h>

int main()
{
int x = 12, y = 1;

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


printf("Initial value of y = %d\n\n", y);
y = ++x;

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);

// Signal to operating system everything works fine


return 0;
}

Postfix Increment/Decrement operator


The postfix increment/decrement operator causes the current value of the variable to
be used in the expression, then the value is incremented or decremented. For
example:
y = x++;
Here first, the current value of x is assigned to y then x is incremented.

Similarly, in the statement:

y = x--;

the current value of x is assigned to y then x is decremented.

The following program demonstrates postfix increment/decrement operator in


action:

#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;
}

You might also like