Unit 2 Notes
Unit 2 Notes
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
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.
Types of C Relational Operators
There are a total of 6 relational operators in C language. There are:
1. Equal to operator (==)
The C equal to operator (==) is a relational operator that is used to check
whether the two given operands are equal or not.
Equal to operator is a binary operator hence it requires two operands to
perform the comparison.
If the two values are equal, it returns true. Otherwise, it returns false.
It does not work for strings or arrays.
Syntax
operand1 == operand2
For example, 5==5 will return true.
2. Not equal to operator (!=)
The C not equal (==) to operator is another relational operator used for
checking whether the two given operands are equal or not.
It is also a binary operator, requiring two operands to perform the
comparison.
It is the exact boolean complement of the ‘==’ operator which returns true
if the two values are not equal, false otherwise.
Syntax
operand1 != operand2
For example, 5!=5 will return false.
3. Greater than operator (>)
The greater than operator is a relational operator in C that checks whether the
first operand is greater than the second operand or not.
It is a binary operator.
If the operand first is greater than the operand2, it returns true. Otherwise,
it returns false.
This operator is used to make decisions or create conditions based on the
relative magnitude of two values.
Syntax
operand1 > operand2
For example, 6>5 will return true.
4. Less than operator (<)
The less than operator is a relational operator in C that checks whether the first
operand is lesser than the second operand.
It is a binary operator.
If the operand first is less than the operand2, it returns true. Otherwise, it
returns false.
This operator is also used to make decisions or create conditions based on
the relative magnitude of two values.
Syntax
operand1 < operand2
For example, 6<5 will return false.
5. Greater than or equal to operator (>=)
The greater than or equal to the operator is a relational operator in C that
checks whether the first operand is greater than or equal to the second operand.
It is a binary operator.
If the operand first is greater than or equal to the operand2, it returns true.
Otherwise, it returns false.
Syntax
operand1 >= operand2
For example, 5>=5 will return true.
6. Less than or equal to the the operator (<=)
The less than or equal to the operator is a relational operator in C that checks
whether the first operand is less than or equal to the second operand.
It is a binary operator.
If the operand first is greater than or equal to the operand2, it returns true.
Otherwise, it returns false.
Syntax
operand1 <= operand2
For example, 5<=5 will also return true.
#include <stdio.h>
int main()
int a = 10, b = 4;
// greater than example
if (a > b)
else
if (a >= b)
else
if (a < b)
else
if (a <= b)
else
// equal to
if (a == b)
// not equal to
if (a != b)
else
return 0;
If both operands are non-zero then the condition becomes true. Otherwise, the
result has a value of 0. The return type of the result is int. Below is the truth
table for the logical AND operator.
X && Y
X Y
1 1 1
1 0 0
0 1 0
0 0 0
2. Logical OR Operator ( || )
X Y X || Y
1 1 1
1 0 1
0 1 1
0 0 0
Syntax
(operand_1 || operand_2)
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0) {
printf("Any one of the given value is "
"greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
If the condition is true then the logical NOT operator will make it false and
vice-versa. Below is the truth table for the logical NOT operator.
X !X
0 1
1 0
Syntax
!(operand_1 && operand_2)
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if !(a > 0 || b > 0) {
printf("Any one of the given value is "
"greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
2. “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator
first adds the current value of the variable on left to the value on the right and
then assigns the result to the variable on the left. Example:
(a += b) can be written as (a = a + b)
3. “-=” This operator is combination of ‘-‘ and ‘=’ operators. This operator
first subtracts the value on the right from the current value of the variable on
left and then assigns the result to the variable on the left. Example:
(a -= b) can be written as (a = a - b)
4. “*=” This operator is combination of ‘*’ and ‘=’ operators. This operator
first multiplies the current value of the variable on left to the value on the right
and then assigns the result to the variable on the left. Example:
(a *= b) can be written as (a = a * b)
5. “/=” This operator is combination of ‘/’ and ‘=’ operators. This operator
first divides the current value of the variable on left by the value on the right
and then assigns the result to the variable on the left. Example:
(a /= b) can be written as (a = a / b)
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a);
a += 10;
printf("Value of a is %d\n", a);
a -= 10;
printf("Value of a is %d\n", a);
a *= 10;
printf("Value of a is %d\n", a);
a /= 10;
printf("Value of a is %d\n", a);
return 0;
}
1. The ^ (bitwise XOR) in C takes two numbers as operands and does XOR
on every bit of two numbers. The result of XOR is 1 if the two bits are
different.
1. The << (left shift) in C takes two numbers, the left shifts the bits of the
first operand, and the second operand decides the number of places to
shift.
1. The >> (right shift) in C takes two numbers, right shifts the bits of the first
operand, and the second operand decides the number of places to shift.
1. The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Unary Operators
2.5 Increment and Decrement operators
The increment ( ++ ) and decrement ( — ) operators in C are unary
operators for incrementing and decrementing the numeric values by
1 respectively.
Syntax of Increment Operator
Increment Operator can be used in two ways which are as follows:
// AS PREFIX
++m
// AS POSTFIX
m++
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.
Syntax
Just like the increment operator, the decrement operator can also be used in
two ways:
// AS PREFIX
--m
// AS POSTFIX
m--
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--;
#include<stdio.h>
int main()
{
int a = 5;
int b = 5;
int prefix = ++a;
printf("Prefix Increment: %d\n", prefix);
printf("Prefix Increment: %d\n", a);
int postfix = b++;
printf("Postfix Increment: %d \n", postfix);
printf("Postfix Increment: %d \n", b);
return 0;
}
#include<stdio.h>
int main()
{
int a = 5;
int b = 5;
int prefix = --a;
printf("Prefix Increment: %d\n", prefix);
printf("Prefix Increment: %d\n", a);
int postfix = b--;
printf("Postfix Increment: %d \n", postfix);
printf("Postfix Increment: %d \n", b);
return 0;
}
Sizeof operator.
It is compile time operator and when used with an operand it returns the no of
bytes the operand occupies.
M=sizeof(sum);
N=sizeof(a);
#include <stdio.h>
int main()
{
int a;
float b;
char c;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(c));
return 0;
}
1. Type Conversion
2. Type Casting
Type Conversion
The type conversion is the process of converting a data value from one data type
to another data type automatically by the compiler. Sometimes type conversion
is also called implicit type conversion. The implicit type conversion is
automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a
float variable the integer value automatically gets converted to float value by
adding decimal value 0. And when a float value is assigned to an integer
variable the float value automatically gets converted to an integer value by
removing the decimal value. To understand more about type conversion observe
the following...
Int i=10;
Float x = 20.9;
Char ch = ‘A’;
Ternary Operator:
2.6 Conditional operators
It is also known as the ternary operator in C as it operates on three operands.
variable = Expression1 ? Expression2 : Expression3;
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
}
#include <stdio.h>
int main()
{
// Verifying the result of the same expression
printf("100 / 5 % 2 = %d", 100 / 5 % 2);
return 0;
}
Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Show Examples
There is also a list of math functions available, that allows you to perform
mathematical tasks on numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
square Root
Example
printf("%f", sqrt(16));
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and
the floor() method rounds a number downwards to its nearest integer, and
returns the result:
Example
printf("%f", ceil(1.4));
printf("%f", floor(1.4));
Power
Example
printf("%f", pow(4, 3));
#include <math.h>
#include <stdio.h>
int main()
{
printf("value1 = %f\n",sqrt(16));
printf("value2 = %f\n",pow(4,2));
printf("value3 = %f\n",ceil(12.8));
printf("value4 = %f\n",floor(12.4)); return (0); }