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.
It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement.
The Arithmetic Operators in C and C++ include:
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;}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 |
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.
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;}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 |
It refers to the boolean values which can be expressed as:
Logical Operators in C/C++ Includes –
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 |
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.
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 |
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 –
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:
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
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
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
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;}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:
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...