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

Operators in C-1

Operator in c programming

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Operators in C-1

Operator in c programming

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OPERATORS IN C

Operators are used to perform operations on variables and values.


1. Unary operator
2. Arithmetic operator
3. Relational operator
4. Logical operator
5. Assignment operator
6. Conditional operator
7. Bitwise operator
1. Unary operator
These are the type of operators that act upon just a single operand for producing a new value.
Types of Unary Operators
1.1 Unary Minus(-)
This operator changes the sign of any given argument. It means that a positive number will
become negative, and a negative number will become positive.
int p = 20;
int q = -p; // q = -20

1.2 Unary Plus(+)


This operator changes the sign of any negative argument. It means that a negative number
will become positive, and a positive number will also become positive.
int p = -20;
int q = +p; // q = 20
1.3 NOT(!)
We use this operator for reversing the logical state of the available operand. It means that the
logical NOT operator will make an available condition to be false if it is already true.
In simple words,

• If p is true, then !p will be false.


• If p is false, then !p will be true.

1.4 Increment
We use this operator to increment the overall value of any given variable by a value of 1.
Prefix Increment
The operator in this method precedes the given operand (Example, ++p). Thus, the value of
the operand gets altered before we finally use it.
int p = 1;
int q = ++p; // q = 2
Postfix Increment
The operator in this method follows the given operand (Example, p++). Thus, the value of the
available operand gets altered after we use it.
For instance,
int p = 1;
int q = p++; // q = 1
int r = p; // r = 2
1.5 Decrement
We use this operator to decrement the overall value of any given variable by a value of 1. We
can perform decrement using two major ways:
Prefix Decrement
The operator in this method precedes the given operand (Example, –p). Thus, the value of the
operand gets altered before we finally use it.
For instance,
int p = 1;
int q = –p; // q = 0
Postfix Decrement
The operator in this method follows the given operand (Example, p–). Thus, the value of the
available operand gets altered after we use it.
For instance,
int p = 1;
int q = p–; // q = 1
int r = p; // r = 0
1.6 Address of Operator(&)
This type of operator provides the user with the address of any variable. The address of
operator is used to return the address (memory address) of any variable.
1.7 Sizeof()
The function of the size of operator is to return the original size of the available operand in
terms of bytes.
2. Arithmetic operator

3. Relational operator
Operator Meaning
== Equal to
!= Not equal to
< Is less than
<= Is less than or equal to
>= Is greater than or equal to
> Is greater than

4. Logical operator
5. Assignment operator

6. Conditional operator
Expression1 ? expression2: expression3;
o If the expression1 results into a true value, then the expression2 will execute.
o If the expression1 returns false value then the expression3 will execute.

#include <stdio.h>
int main()
{
int age;
printf("Enter your age");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));
return 0;
}
7. Bitwise operator
The bitwise operators are the operators used to perform the operations on the data at the bit-
level.

Question to practice: What is the output of following programs


//Program 1
#include <stdio.h>
int main()
{
int d, a = 1, b = 2, x;
d= a++;
x= ++b;
printf("%d %d %d %d", d, a, b, x);
}
//Program 2
#include <stdio.h>
int main ()
{
int x=21, y=15, z=13;
--x;
--y;
--z;
printf (" \n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
//Program 3
#include <stdio.h>
int main ()
{
int x=100, y=200;
printf(“%d”,(x>y)? x:y);
}

You might also like