Unit 2
Operators and Expressions
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.
For example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands
‘a’ and ‘b’.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6
types based on their functionality:
1. Arithmetic Operators
1. Relational Operators
1. Logical Operators
1. Bitwise Operators
1. Assignment Operators
1. Other Operators
,2.1 Arithmetic operators
Arithmetic operators are used to perform common mathematical operations
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
Integer Arithmetic
When both the operands are integer.
a-b=10
a+b=18
Real Arithmetic
When both the operands are real.
x=6.0/7.0
y=1.0/3.0
Mixed Mode Arithmetic
When one operand is real and another is integer.
x=15/10.0
y=15/10
, #include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a + b is %d\n", res);
res = a - b; // subtraction
printf("a - b is %d\n", res);
res = a * b; // multiplication
printf("a * b is %d\n", res);
res = a / b; // division
printf("a / b is %d\n", res);
res = a % b; // modulus
printf("a %% b is %d\n", res);
return 0;
}
2.2 Relational operators
relational operators are the symbols that are used for comparison between
two values to understand the type of relationship a pair of numbers shares.
The result that we get after the relational operation is a boolean value, that tells
whether the comparison is true or false.
Relational operators are mainly used in conditional statements and loops to
check the conditions in C programming.
Operators and Expressions
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.
For example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands
‘a’ and ‘b’.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6
types based on their functionality:
1. Arithmetic Operators
1. Relational Operators
1. Logical Operators
1. Bitwise Operators
1. Assignment Operators
1. Other Operators
,2.1 Arithmetic operators
Arithmetic operators are used to perform common mathematical operations
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
Integer Arithmetic
When both the operands are integer.
a-b=10
a+b=18
Real Arithmetic
When both the operands are real.
x=6.0/7.0
y=1.0/3.0
Mixed Mode Arithmetic
When one operand is real and another is integer.
x=15/10.0
y=15/10
, #include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a + b is %d\n", res);
res = a - b; // subtraction
printf("a - b is %d\n", res);
res = a * b; // multiplication
printf("a * b is %d\n", res);
res = a / b; // division
printf("a / b is %d\n", res);
res = a % b; // modulus
printf("a %% b is %d\n", res);
return 0;
}
2.2 Relational operators
relational operators are the symbols that are used for comparison between
two values to understand the type of relationship a pair of numbers shares.
The result that we get after the relational operation is a boolean value, that tells
whether the comparison is true or false.
Relational operators are mainly used in conditional statements and loops to
check the conditions in C programming.