PROGRAMMING IN C
C Operators
C uses operators to perform arithmetic operations on variables and their
values.
C divides the operators into the following groups:
Arithmetic operators
Unary Operators
Relational Operator
Logical operators
Assignment operators
Comparison operators
Bitwise operators
Arithmetic operators
Arithmetic operators are used to perform common mathematical operations.
Addition Multiplication
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 5; int x = 5;
int y = 3; int y = 3;
printf("%d", x + y); printf("%d", x * y);
return 0; return 0;
} }
Subtraction Division
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 5;
int x = 12;
int y = 3;
printf("%d", x - y);
int y = 3;
return 0; printf("%d", x / y);
} return 0;
}
Modulus Decrement
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 5; int x = 5;
int y = 2; printf("%d", --x);
printf("%d", x % y);
return 0;
return 0;
}
}
Increment
#include <stdio.h>
int main() {
int x = 5;
printf("%d", ++x);
return 0;
}
Unary
A unary operator is an operator used to operate on a single operand to return
a new value.
In other words, it is an operator that updates the value of an operand or
expression's value by using the appropriate unary operators.
In Unary Operator, operators have equal priority from right to left side
associativity.
Types of the Unary Operator
Following are the types of the unary operators in the C programming
language.
Unary Minus (-)
Unary Plus (+)
Increment (++)
Decrement (--)
Logical Negation (!)
AddressOf Operator (&)
Sizeof() operator
Unary Minus (-)
The Unary Minus operator is represented using the symbol (-). The unary operator is used to
change the sign of any positive value to a negative value. It means it changes the positive
number to the negative, and a negative number becomes the positive number using the unary
minus operator.
Syntax
1. int a = 2;
2. int b = -(a);
▪ In the next slide will introduce the #include<conio.h>. It is a header file is a C library that provides functions
for handling console input and output operations.
Unary Minus (-)…
printf (" The value of a: %d \n", a);
#include <stdio.h> printf (" The value of b: %d \n", b);
#include <conio.h> printf (" The value of -n1: %d \n", -n1);
int main () printf (" The value of -n2: %d ", -n2);
{
int a = 5; // positive value of a. return 0;
int b = -(a); // use unary minus }
operator to change the value
int n1 = 20;
int n2 = -30;
Unary plus (+) #include <stdio.h>
#include <conio.h>
int main ()
{
The unary plus operator is
int a = 10; // use unary plus operator
represented as the "+" symbol, and
it does not change to the operand int b = (-10); // It does not change the operand
value value
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
return 0;
}
Unary Increment Operator (++)
It is the unary increment operator, which is denoted by the "++" symbol.
The "++" symbol represents the operand's value is increased by 1. It can be used in two ways, as
the post-increment and the pre-increment.
Pre Increment: The pre-increment operator is represented as (++a), which means the value of
variable 'a' is increment by 1 before using operand to the expression.
For example:
1. x = 10;
2. A = ++x;
Here the initial value of the x variable is 10 and using the post-increment operator (x++) to assign
increment value of the 'x' to the variable 'A'.
Unary Increment Operator (++)…
#include <stdio.h> printf (" \n The value of a is %d.", a);
#include <conio.h>
int main () b = 20;
{ y = b++; // It shows the post increment
operator
int x, y, a, b; // declare local variable
printf (" \n\n Post Increment Operator");
a = 10;
printf (" \n The value of y is %d.", y);
x = ++a; // It shows pre increment operator
// get updated value of b
printf (" \n The value of b is %d.", b);
printf (" Pre Increment Operator");
// Here the value of x is increased by 1.
return 0;
printf (" \n The value of x is %d.", x);
}
Unary Decrement Operator (--)
The unary decrement operator is opposite to the unary increment operator. T
he Unary decrement operator is represented by the double minus (--) symbol,
and it is used to decrease the operand value by 1 according to the
decrement's types.
The Unary decrement operator is of two types: the Pre decrement operator
and the Post Decrement operator.
Pre decrement operator and Post Decrement operator
Pre Decrement: The pre decrement operator is denoted as (--a) symbol, meaning the operand
value is decreased by 1 before assigning to another variable or expression.
Syntax
1. int pre = --a;
Post Decrement: The Post decrement operator is denoted as (a--) symbol, which means the
original value is decreased by 1 after assigning to another variable or expression.
Syntax
1. int post = a--;
Pre decrement operator and Post Decrement operator
#include <stdio.h> printf (" \n The value of a is %d.", a);
#include <conio.h>
int main () b = 20;
{ y = b--; // It shows the post decrement
operator
int x, y, a, b; // declare local variable
printf (" \n\n Post Decrement Operator");
a = 10;
printf (" \n The value of y is %d.", y);
x = --a; // It shows pre decrement operator
// get updated value of b
printf (" \n The value of b is %d.", b);
printf (" Pre Decrement Operator");
// Here the value of x is decreased by 1.
return 0;
printf (" \n The value of x is %d.", x);
}
Unary Sizeof() Operator
The sizeof is a keyword used to find the size of different data types or operands like int,
float, char, double, etc.
Syntax
1. sizeof(data_variable);
Unary Sizeof() Operator…
#include <stdio.h> // use sizeof() operator and pass the different data type variable to get their size.
#include <conio.h> printf (" The size of the int (x) variable is: %d", sizeof(x));
int main () printf (" \n The size of the float (y) variable is: %d", sizeof(y));
{ printf (" \n The size of the char (ch) variable is: %d", sizeof(ch));
int x; printf (" \n The size of the double (z) variable is: %d", sizeof(z));
float y; return 0;
char ch; }
double z;
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.
Relational Operators…
#include <stdio.h>
int main()
Operator Meaning of Operator Example {
int a = 5, b = 5, c = 10;
5 == 3 is evaluated to
== Equal to
0 printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
5 > 3 is evaluated to printf("%d > %d is %d \n", a, b, a > b);
> Greater than
1
printf("%d > %d is %d \n", a, c, a > c);
< Less than
5 < 3 is evaluated to printf("%d < %d is %d \n", a, b, a < b);
0
printf("%d < %d is %d \n", a, c, a < c);
5 != 3 is evaluated to printf("%d != %d is %d \n", a, b, a != b);
!= Not equal to
1 printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
Greater than or 5 >= 3 is evaluated to
>= printf("%d >= %d is %d \n", a, c, a >= c);
equal to 1
printf("%d <= %d is %d \n", a, b, a <= b);
5 <= 3 is evaluated to
<= Less than or equal to
0 printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Operator Name Description Example
&& Logical and Returns true if both x < 5 && x < 10
statements are true
|| Logical or Returns true if one of the x < 5 || x < 4
statements is true
! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Logical Operators…
#include <stdio.h> result = (a != b) || (c < b);
int main() printf("(a != b) || (c < b) is %d \n", result);
{
int a = 5, b = 5, c = 10, result; result = !(a != b);
printf("!(a != 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) && (c < b);
printf("(a == b) && (c < b) is %d \n", result); return 0;
}
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
Explanation of logical operator program
(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b)
is 1 (true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are
0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b)
is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0
(false).
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
int x = 5;
int y = 3; // Returns 1 (true) because 5 is greater than 3
AND 5 is less than 10 int y = 3; // Returns false (0) because ! (not) is
printf("%d", x > 3 && x < 10); used to reverse the result
return 0; printf("%d", !(x > 3 && x < 10));
} return 0;
}
#include <stdio.h>
int main() {
int x = 5;
int y = 3; // Returns 1 (true) because one of
the conditions are true (5 is greater than 3,
but 5 is not less than 4)
printf("%d", x > 3 || x < 4);
return 0;
}
Assignment operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable
called x:
A list of all assignment operators:
Operator Example Same As Operator Example Same A
= x=5 x=5 %= x %= 3 x=x%3
+= x += 3 x=x+3 &= x &= 3 x=x&3
-= x -= 3 x=x-3 |= x |= 3 x=x|3
*= x *= 3 x=x*3 ^= x ^= 3 x=x^3
/= x /= 3 x=x/3 >>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
int x = 5;
x -= 3;
printf("%d", x);
printf("%d", x);
return 0;
return 0;
}
}
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
int x = 5;
x += 3;
x *= 3;
printf("%d", x);
printf("%d", x);
return 0;
return 0;
}
}
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int x = 5;
float x = 5;
x &= 3;
x /= 3;
printf("%d", x);
printf("%f", x);
return 0;
return 0;
}
}
#include <stdio.h>
int main() {
int x = 5;
x %= 3;
printf("%d", x);
return 0;
}
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 5; int x = 5;
x |= 3; x >>= 3;
printf("%d", x); printf("%d", x);
return 0; return 0;
} }
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 5; int x = 5;
x ^= 3; x <<= 3;
printf("%d", x); printf("%d", x);
return 0; return 0;
} }
Assignment operators…
// Working of assignment operators printf("c = %d\n", c);
#include <stdio.h> c *= a; // c is 25
int main() printf("c = %d\n", c);
{ c /= a; // c is 5
int a = 5, c; printf("c = %d\n", c);
c = a; // c is 5 c %= a; // c = 0
printf("c = %d\n", c); printf("c = %d\n", c);
c += a; // c is 10 return 0;
printf("c = %d\n", c); }
c -= a; // c is 5
Comparison operators
Comparison operators are used to compare two values (or variables).
The return value of a comparison is either 1 or 0, which means true (1) or false (0). These values
are known as Boolean values.
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
#include <stdio.h>
int main() { #include <stdio.h>
int x = 5; int main() {
int y = 3; int x = 5;
printf("%d", x == y); // returns 0 (false) because 5 int y = 3;
is not equal to 3
printf("%d", x > y); // returns 1 (true) because 5 is
return 0; greater than 3
} return 0;
}
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x != y); // returns 1 (true) because 5
is not equal to 3
return 0;
}
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x < y); // returns 0 (false) because
5 is not less than 3
return 0; #include <stdio.h>
} int main() {
int x = 5;
#include <stdio.h> int y = 3; // Returns 0 (false) because 5
is neither less than or equal to 3
int main() {
printf("%d", x <= y);
int x = 5;
return 0;
int y = 3;
}
// Returns 1 (true) because five is greater
than, or equal, to 3
printf("%d", x >= y);
return 0;
}
Bitwise operators
The bitwise operators are used to perform bit-level operations on operands.
These operators allow the evaluation and manipulation of specific bits within
the integer.
Bitwise operators are used on (binary) numbers:
Bitwise operators…
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
Let's look at 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
Bitwise operators…
Bitwise AND operator Bitwise OR operator
1. #include <stdio.h> 1. #include <stdio.h>
2. int main() 2. int main()
3. { 3. {
4. int a=6, b=14; 4. int a=23,b=10;
5. printf("The output is: %d",a&b); 5. printf("The output is: %d",a|b);
6. return 0; 6. return 0;
7. } 7. }
Bitwise operators…
Bitwise exclusive OR operator One's complement operator (unary
operator)
1. #include <stdio.h>
1. #include <stdio.h>
2. int main()
2. int main()
3. {
3. {
4. int a=12,b=10;
4. int a=8;
5. printf("The output is: %d",a^b);
5. printf("The output of the Bitwise compl
6. return 0;
ement operator ~a is %d",~a);
7. } 6. return 0;
7. }
Bitwise operators…
Left shift operator Right shift operator
1. #include <stdio.h> 1. #include <stdio.h>
2. int main() 2. int main()
3. { 3. {
4. int a=5; 4. int a=7;
5. printf("The value of a<<2 is : %d ", a< 5. printf("The value of a>>2 is : %d ", a>
<2); >2);
6. return 0; 6. return 0;
7. } 7. }
Logical Not (!) Operator
The logical not operator is used to reverse the given condition. For example, if the
operand is true, the logical not operator (!) reverses and return false; if the operand is
false, the logical operator returns true.
Syntax
1. bool a = true;
2. bool b = !a; // It reverse the condition of variable b
Logical Not (!) Operator…
#include <stdio.h> printf (" The Boolean value of a is: %d", a);
#include <stdbool.h> printf (" \n The Boolean value of b is: %d", b);
int main ()
{ bool c = 0;
// declare variables bool d = !c;
bool a = true; printf (" \n The Boolean value of c is: %d", c);
bool b; printf (" \n The Boolean value of d is: %d", d);
b = !a; // use logical operator to reverse the condition return 0;
}
AddressOf Operator (&)
The Unary AddressOf Operator is denoted as ampersand (&) symbol, which is used to
find the address of a variable defined in computer memory.
Syntax
1. int a = 5;
2. int b = &a; // variable b hold the address of variable a
AddressOf Operator (&)…
#include <stdio.h> // use addressof (&) operator to assign the
address
#include <conio.h>
b = &a;
int main ()
printf (" The value of variable a is: %d", a);
{
// declare variables
printf (" \n The address of variable b is: %d
int a = 10; ", b);
int b; return 0;
}