0% found this document useful (0 votes)
9 views12 pages

Operators

The document provides an overview of various operators in C programming, including arithmetic, increment/decrement, assignment, relational, logical, bitwise, comma, sizeof, and address operators. Each operator type is explained with definitions and examples to illustrate their usage in mathematical and logical operations. The document also includes sample code snippets demonstrating the functionality of these operators.

Uploaded by

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

Operators

The document provides an overview of various operators in C programming, including arithmetic, increment/decrement, assignment, relational, logical, bitwise, comma, sizeof, and address operators. Each operator type is explained with definitions and examples to illustrate their usage in mathematical and logical operations. The document also includes sample code snippets demonstrating the functionality of these operators.

Uploaded by

srujansamala07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Programming Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.

 Arithmetic Operators
 Increment and decrement operators
 Assignment operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Coma operator
 Sizeof operator

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
1. // Working of arithmetic operators
2. #include <stdio.h>
3. int main()
4. {
5. int a = 9,b = 4, c;
6.
7. c = a+b;
8. printf("a+b = %d \n",c);
9. c = a-b;
10. printf("a-b = %d \n",c);
11. c = a*b;
12. printf("a*b = %d \n",c);
13. c = a/b;
14. printf("a/b = %d \n",c);
15. c = a%b;
16. printf("Remainder when a divided by b = %d \n",c);
17.
18. return 0;
19. }
20. Output
21. a+b = 13
22. a-b = 5
23. a*b = 36
24. a/b = 2
25. Remainder when a divided by b=1

Unary plus (+) Operator


This operator does not make any effect on the operand value, it just returns
operands value.

Consider the given example:


#include <stdio.h>
int main()
{
int x=1;
x= +4;
printf("x= %d\n",x);
return 0;
}

Unary minus (-) Operator


This operator makes the value negative. It makes positive value to negative
and negative value to positive.

Consider the given example:

#include <stdio.h>
int main()
{
int x=10;
int y=-20;
printf("value of -x: %d\n",-x);
printf("value of -y: %d\n",-y);
return 0;

C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value
of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single
operand.

Example 2: Increment and Decrement Operators


1. // Working of increment and decrement operators
2. #include<stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

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


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b

Example 3: Assignment Operators


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

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 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

5 > 3 is greater than to


> Greater than 1

< Less than 5 < 3 is less than to 0

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


Operator Meaning of Operator Example

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

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

Example 4: 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 If c = 5 and d = 2 then, expression ((c==5) &&
&& operands are true (d>5)) equals to 0.

Logical OR. True only if either If c = 5 and d = 2 then, expression ((c==5) ||


|| one operand is true (d>5)) equals to 1.

Logical NOT. True only if the


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

Example 5: 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;
}

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

C Bitwise Operators

During computation, mathematical operations like: addition, subtraction, multiplication,


division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left


Operators Meaning of operators

>> Shift right

Example 1: Bitwise AND

#include <stdio.h>
int main() {
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Output:8
Bitwise OR Operator |
#include <stdio.h>

int main() {

int a = 12, b = 25;

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

return 0;

Output:29
Bitwise XOR
#include <stdio.h>

int main() {

int a = 12, b = 25;

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

return 0;

Output:21

Other Operators

Comma Operator

Comma operators are used to link related expressions together. For example:

1. int a, c = 5, d;

The sizeof operator


The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).

Example 6: sizeof Operator


#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Address of (&) Operator


This operator returns address of any variable.

Consider the given example

#include <stdio.h>

int main()
{
int x=10;

printf("Value of x: %d\n",x);
printf("Address of x: %X\n",&x);

return 0;
}

You might also like