5 Computer -- Programming II

Define operators. Explain types of operators.

Define operators. Explain types of operators.

Operators can be defined as basic symbols that help us work on logical and mathematical operations. Operators in C and C++, are tools or symbols that are used to perform mathematical operations concerning arithmetic, logical, conditional and, bitwise operations.


1. Arithmetic Operators

It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement.

The Arithmetic Operators in C and C++ include:

  1.  + (Addition) – This operator is used to add two operands.
  2. – (Subtraction) – Subtract two operands.
  3. * (Multiplication) – Multiply two operands.
  4. / (Division) – Divide two operands and gives the quotient as the answer.
  5. % (Modulus operation) – Find the remains of two integers and gives the remainder after the division.
  6. ++ (Increment) – Used to increment an operand.
  7. — (Decrement) – Used to decrement an operand.
Example of Arithmetic Operators in C#include<stdio.h>int main(){int a=10, b=7;printf("Welcome to DataFlair tutorials!\n\n");printf("The Addition of %d and %d is: %d\n",a,b,a+b);printf("The Subtraction of %d and %d is: %d\n",a,b,a-b);printf("The Multiplication of %d and %d is: %d\n",a,b,a*b);printf("The Division of %d and %d is: %d\n",a,b,a/b);printf("The Modulus operation of %d and %d is: %d\n",a,b,a%b);printf("The Incremented value ++a is: %d\n",++a);printf("The Decremented value --b is: %d\n",--b);return 0;}

Example of Arithmetic Operators in C++

Here is a code in C++ which illustrates all the basic arithmetic operators:

#include <iostream>using namespace std;int main(){ cout<<"Welcome to DataFlair tutorials!\n\n"<<endl<<endl;int a = 10, b = 7;cout<<"The Addition of "<< a << " and " << b << " are: " << a + b <<endl;cout<<"The Subtraction of "<< a << " and " << b << " are: " << a - b <<endl;cout<<"The Multiplication of "<< a << " and " << b << " are: " << a * b <<endl;cout<<"The Division of "<< a << " and " << b << " are: " << a / b <<endl;cout<<"The Modulus operation between "<< a << " and " << b << " is: " << a % b <<endl;cout<<"The Incremented value ++a is: "<< ++a <<endl;cout<<"The Decremented value --a is: "<< --a <<endl;return 0;}
Table for Arithmetic Operators in C and C++
Operator Operand Operation Elucidation
+ a, b a + b Addition
a, b a – b Subtraction
* a, b a * b Multiplication
/ a, b a / b Division
% a, b a % b Modulus operator – to find the remainder when two integral digits are divided
++ a a ++ Increment
a a – – Decrement
2. Relational Operators

It is used to compare two numbers by checking whether they are equal or not, less than, less than or equal to, greater than, greater than or equal to.

  1. == (Equal to)– This operator is used to check if both operands are equal.
  2. !=  (Not equal to)– Can check if both operands are not equal.
  3. > (Greater than)– Can check if the first operand is greater than the second.
  4. < (Less than)- Can check if the first operand is lesser than the second.
  5. >=  (Greater than equal to)– Check if the first operand is greater than or equal to the second.
  6. <= (Less than equal to)– Check if the first operand is lesser than or equal to the second

If the relational statement is satisfied (it is true), then the program will return the value 1, otherwise, if the relational statement is not satisfied (it is false), the program will return the value 0.

Example of Relational Operators in C-#include <stdio.h>int main(){int a=10, b=10, c=20;printf("Welcome to DataFlair tutorials!\n\n");printf("For %d == %d : The output is: %d \n", a, b, a == b); // condition is trueprintf("For %d == %d : The output is: %d \n", a, c, a == c); // condition is falseprintf("For %d != %d : The output is: %d \n", a, c, a != c); // condition is trueprintf("For %d != %d : The output is: %d \n", a, b, a != b); // condition is falseprintf("For %d > %d : The output is: %d \n", a, b, a > b); // condition is falseprintf("For %d > %d : The output is: %d \n", a, c, a > c); // condition is falseprintf("For %d < %d : The output is: %d \n", a, b, a < b); // condition is falseprintf("For %d < %d : The output is: %d \n", a, c, a < c); // condition is trueprintf("For %d >= %d : The output is: %d \n", a, b, a >= b); // condition is trueprintf("For %d >= %d : The output is: %d \n", a, c, a >= c); // condition is falseprintf("For %d <= %d : The output is: %d \n", a, b, a <= b); // condition is trueprintf("For %d <= %d : The output is: %d \n", a, c, a <= c); // condition is truereturn 0;}

Example of Relational Operators in C++

Here is a code in C++ which illustrates all the basic relational operators:

#include <iostream>using namespace std;int main(){cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;int a = 10, b = 10, c = 20;cout<<"For " << a << " == " << b << " The output is: " << (a == b) << endl; // condition is truecout<<"For " << a << " == " << c << " The output is: " << (a == c) << endl; // condition is falsecout<<"For " << a << " != " << c << " The output is: " << (a != c) << endl; // condition is truecout<<"For " << a << " != " << b << " The output is: " << (a != b) << endl; // condition is falsecout<<"For " << a << " > " << b << " The output is: " << (a > b) << endl; // condition is falsecout<<"For " << a << " > " << c << " The output is: " << (a > c) << endl; // condition is falsecout<<"For " << a << " < " << b << " The output is: " << (a < b) << endl; // condition is falsecout<<"For " << a << " < " << c << " The output is: " << (a < c) << endl; // condition is truecout<<"For " << a << " >= " << b << " The output is: " << (a >= b) << endl; // condition is truecout<<"For " << a << " >= " << c << " The output is: " << (a >= c) << endl; // condition is falsecout<<"For " << a << " <= " << b << " The output is: " << (a <= b) << endl; // condition is truecout<<"For " << a << " <= " << c << " The output is: " << (a <= c) << endl; // condition is truereturn 0;}


Table for Relational Operators in C and C++
 Operator Operand Operation Elucidation
== a, b (a==b) Used to check if both operands are equal
!= a, b (a!=b) Used to check if both operands are not equal
> a, b (a>b) Used to check if the first operand is greater than the second
< a, b (a<b) Used to check if the first operand is lesser than the second
>= a, b (a>=b) Used to check if the first operand is greater than or equal to the second
<= a, b (a<=b) Used to check if the first operand is lesser than or equal to the second
3. Logical Operators

It refers to the boolean values which can be expressed as:

  • Binary logical operations, which involves two variables: AND and OR
  • Unary logical operation: NOT

Logical Operators in C/C++ Includes –

  1. && (AND) – It is used to check if both the operands are true.
  2. || (OR) – These operators are used to check if at least one of the operand is true.
  3. ! (NOT) – Used to check if the operand is false

If the logical statement is satisfied (it is true), then the program will return the value 1, otherwise, if the relational statement is not satisfied (it is false), the program will return the value 0.

Example of Logical Operators in C Programming-#include <stdio.h>int main(){int a = 10, b = 10, c = 20, answer;printf("Welcome to DataFlair tutorials!\n\n");answer = (a == b) && (c > b);printf("For (%d == %d) && (%d != %d), the output is: %d \n",a,b,b,c,answer); //condition is trueanswer = (a == b) && (c < b) && (c>0);printf("For (%d == %d) && (%d <= %d), the output is: %d \n",a,b,b,c,answer); //condition is falseanswer = (a == b) || (b > c);printf("For (%d == %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer); / /condition is trueanswer = (a != b) || (a <= b) || (a>c);printf("For (%d != %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer); //condition is trueanswer = !(a == b);printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is false answer = !(a != b);printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is truereturn 0;}

Table for Logical Operators in C and C++
Operator Operand Operation Elucidation
&& a, b (a && b) AND: Used to check if both the operands are true
|| a, b (a || b) OR: Used to check if at least one of the operand is true
! a !a NOT: Used to check if the operand is false
4. Assignment Operators

It is used to assign a particular value to a variable. We will discuss it in detail in the later section with its shorthand notations.

  1. =  (Assignment)- Used to assign a value from right side operand to left side operand.
  2. += (Addition Assignment)- To store the sum of both the operands to the left side operand.
  3. -= (Subtraction Assignment) – To store the difference of both the operands to the left side operand.
  4. *= (Multiplication Assignment) – To store the product of both the operands to the left side operand.
  5. /= (Division Assignment) – To store the division of both the operands to the left side operand.
  6. %= (Remainder Assignment) – To store the remainder of both the operands to the left side operand.
Example of Assignment Operators in C#include<stdio.h>int main(){printf("Welcome to Dataflair tutorials!\n\n");int number = 10, result;result = number;printf("result = %d \n", result);result += number; //Same as result = result + aprintf("result = %d \n", result);result -= number; //Same as result = result - aprintf("result = %d \n", result);result *= number; //Same as result = result * aprintf("result = %d \n", result);result /= number; //Same as result = result / aprintf("result = %d \n", result);result %= number; //Same as result = result % aprintf("result = %d \n", result);return 0;}

Table for Assignment Operators in C and C++
Operator Operand Operation Elucidation
= a, b a=b Used to assign a value from right side operand to left side operand
+= a, b a+=b a=a+b: The value of a+b is stored in a
-= a, b a-=b a=a-b: The value of a-b is stored in a
*= a, b a*=b a=a*b: The value of a*b is stored in a
/= a, b a/=b a=a/b: The value of a/b is stored in a
%= a, b a%=b a=a %b: The value of a%b is stored in a
5. Bitwise Operators

It is based on the principle of performing operations bit by bit which is based on boolean algebra. It increases the processing speed and hence the efficiency of the program.

The Bitwise Operators in C/C++ Includes –

  1. & (Bitwise AND) – Converts the value of both the operands into binary form and performs AND operation bit by bit.
  2. | (Bitwise OR) – Converts the value of both the operands into binary form and performs OR operation bit by bit.
  3. ^ (Bitwise exclusive OR) – Converts the value of both the operands into binary form and performs EXCLUSIVE OR operation bit by bit.
  4. ~ (One’s complement operator): Converts the operand into its complementary form.
  5. << – Left shift
  6. >> – Right shift

Key takeaway: Bitwise operators are not applicable in the case of float and double data type in C.

In order to clearly understand bitwise operators, let us see the truth table for various bitwise operations and understand how it is associated with boolean algebra.

Since there are 2 variables, namely, a and b, there are 22 combinations for values a and b can take simultaneously.

AND – Both the operands should have boolean value 1 for the result to be 1.

OR – At least one operand should have boolean value 1 for the result to be 1.

XOR (EXCLUSIVE OR) – Either the first operand should have boolean value 1 or the second operand should have boolean value 1. Both cannot have the boolean value 1 simultaneously.

One Complement: iF

a b a & b a | b a ^ b ~a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0

The left and right shift operators are responsible for shifting the binary values by some specific number of places.

Left shift: It specifies the value of the left operand to be shifted to the left by the number of bits specified by its right operand

Right shift: It species the value of the left operand to be shifted to the right by the number of bits specified by its right operand.

Let us take an example each of performing bitwise AND, OR, EXCLUSIVE OR and ONE’S COMPLEMENT operation.

Consider two operands, a and b with values:

a = 26 and b=14

Therefore, a & b is computed as follows:

  1. Find the binary equivalent of a and b:
  2. Perform boolean AND/OR/EXCLUSIVE OR operation bit by btw
  3. Convert the answer into its corresponding decimal form.
  • Bitwise AND

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a & b = 0 1 0 1 0 which is equal to 10

  • Bitwise OR

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 1 1 1 0 which is equal to 30

  • Bitwise XOR

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 0 1 0 0 which is equal to 20

  • Bitwise One’s Complement

a = 26 = 1 1 0 1 0

Reversing its bits, we get 0 0 1 0 1 which is equal to 5 but this is not the correct answer! The correct answer is: -(a+1) which is -27 which is in accordance with two’s complement.

Example of Bitwise Operators in C#include <stdio.h>int main(){printf("Welcome to DataFlair tutorials!\n\n");int a = 26, b = 14;printf(" Bitwise AND operation %d & %d : %d\n",a,b,a&b);printf(" Bitwise OR operation %d | %d : %d\n",a,b,a|b);printf(" Bitwise XOR operation %d ^ %d : %d\n",a,b,a^b);printf(" Bitwise ONE'S COMPLEMENT ~ %d operation : %d\n",a,~a);return 0;}


Table for Bitwise Operators in C and C++
Operator Operand Operation Elucidation
& a, b ( a & b ) Bitwise AND: Converts the value of both the operands into binary form and performs AND- operation bit by bit
| a, b ( a | b ) Bitwise OR:
Converts the value of both the operands into binary form and performs OR- operation bit by bit
^ a, b ( a ^ b ) Bitwise exclusive OR: Converts the value of both the operands into binary form and performs EXCLUSIVE OR operation bit by bit
~ a ( ~ a ) One’s complement operator: Converts the operand into its complementary form
<< a a<< Left shift
>> a a>> Right shift

 

6. Miscellaneous Operators

Apart from the above-discussed operators, there are certain operators which fall under this category which include sizeof and ternary (conditional) operators.

Here is a table which illustrates the use of these operators:

  1. sizeof – It returns the memory occupied by the particular data type of the operand
  2. & (Pointer) – It refers to the address (memory location) in which the operand is stored.
  3. *  (Pointer) –  It is a pointer operator
  4. ? (Condition) – It is an alternative for if-else condition
Example of Miscellaneous Operators in C#include<stdio.h>int main(){printf("Welcome to Data Flair tutorials!\n\n");// Use of * and & operatorint number = 10, *pointer;pointer=&number; //Here the pointer stores the memory address of variable numberprintf("The value of the number is: %d\n",*pointer);// Use of ?: operatorint expression1 = 10, expression2 = 20, expression3;expression3 = ( expression1 > expression2 ) ? expression1 : expression2;printf("The Output of the ternary statement is: %d", expression3);return 0;}



More questions on Programming II

Differentiate between structure and union.

Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.

Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:

struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...
Close Open App