Contents
Operators
Operators in C++
An operator is a symbol that acts on a value to perform specific mathematical or logical operations, forming the core of any programming language. In C++, a range of built-in operators is available to perform essential tasks and calculations.
Example:
c = a + b;
In this example, the +
symbol is the addition operator, while a
and b
are the operands. The addition operator tells the compiler to sum the values of a
and b
.
Types of Operators in C
The C language provides a wide array of operators, categorized into six types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Miscellaneous Operators
1. Arithmetic Operators in C : Arithmetic operators allow us to perform standard mathematical operations. In C, there are 9 arithmetic operators:
Symbol | Operator | Description | Syntax |
---|---|---|---|
| Addition | Adds two numeric values. |
|
| Subtraction | Subtracts the right operand from the left. |
|
| Multiplication | Multiplies two numeric values. |
|
| Division | Divides two numeric values. |
|
| Modulus | Gives the remainder of a division. |
|
| Unary Plus | Specifies positive values. |
|
| Unary Minus | Reverses the sign of a value. |
|
| Increment | Increases the value of a variable by 1. |
|
| Decrement | Decreases the value of a variable by 1. |
|
Example:
#include
int main() {
int a = 25, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
Output:
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26
2. Relational Operators in C : Relational operators are used to compare two operands. These operators return a boolean value (true
or false
), depending on the comparison result.
Symbol | Operator | Description | Syntax |
---|---|---|---|
< | Less than | Returns true if the left operand is less than the right. | a < b |
> | Greater than | Returns true if the left operand is greater than the right. | a > b |
<= | Less than or equal to | Returns true if the left operand is less than or equal to the right. | a <= b |
>= | Greater than or equal to | Returns true if the left operand is greater than or equal to the right. | a >= b |
== | Equal to | Returns true if both operands are equal. | a == b |
!= | Not equal to | Returns true if the operands are not equal. | a != b |
Example:
#include
int main() {
int a = 25, b = 5;
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}
Output:
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operators in C : Logical operators are used to combine multiple conditions. They evaluate to a boolean value (true
or false
).
Symbol | Operator | Description | Syntax |
---|---|---|---|
&& | Logical AND | Returns true if both operands are true. | a && b |
` | ` | Logical OR | |
! | Logical NOT | Returns true if the operand is false. | !a |
Example:
#include
int main() {
int a = 25, b = 5;
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Output:
a && b : 1
a || b : 1
!a: 0
C++ sizeof Operator
The sizeof
Operator in C++
The sizeof
operator is a unary operator used during compilation to determine the size of variables, data types, constants, classes, structures, and unions in bytes.
Syntax:
sizeof(data_type)
Example 1:
The following program shows how to use sizeof
to find the size of various data types.
#include
using namespace std;
int main()
{
cout << "Bytes taken up by short is " << sizeof(short) << endl;
cout << "Bytes taken up by long long is " << sizeof(long long) << endl;
cout << "Bytes taken up by bool is " << sizeof(bool) << endl;
cout << "Bytes taken up by wchar_t is " << sizeof(wchar_t) << endl;
return 0;
}
Output:
Bytes taken up by short is 2
Bytes taken up by long long is 8
Bytes taken up by bool is 1
Bytes taken up by wchar_t is 4
Example 2:
This example demonstrates the size of variables of different data types using the sizeof
operator.
#include
using namespace std;
int main()
{
double pi;
bool flag;
char grade;
cout << "Bytes taken up by pi is " << sizeof(pi) << endl;
cout << "Bytes taken up by flag is " << sizeof(flag) << endl;
cout << "Bytes taken up by grade is " << sizeof(grade) << endl;
return 0;
}
Output:
Bytes taken up by pi is 8
Bytes taken up by flag is 1
Bytes taken up by grade is 1
Example 3:
This example demonstrates using sizeof
to find the size of expressions.
#include
int main() {
int result;
// Expression with multiple operators
result = 10 * 5 + 8 / 2;
printf("Result = %d\n", result);
return 0;
}
Output:
Bytes taken up by (m + n) is 4
Bytes taken up by (m + x) is 8
Bytes taken up by (n + x) is 8
Output:
Bytes taken up by (m + n) is 4
Bytes taken up by (m + x) is 8
Bytes taken up by (n + x) is 8
Example 4:
This example uses sizeof
to calculate the size of an array.
#include
using namespace std;
int main()
{
double values[] = {2.3, 4.6, 5.1, 8.9, 10.0};
int length = sizeof(values) / sizeof(values[0]);
cout << "Length of the array is " << length << endl;
return 0;
}
Output:
Length of the array is 5
Example 5:
This example shows how to use sizeof
to find the size of a class.
#include
using namespace std;
class SampleClass
{
char id;
int count;
};
int main()
{
SampleClass obj;
cout << "Size of SampleClass object is: " << sizeof(obj) << " bytes" << endl;
return 0;
}
Output:
Size of SampleClass object is: 8 bytes
Example 6:
This example demonstrates the size of different pointers using the sizeof
operator.
#include
using namespace std;
int main()
{
int *ptrInt = new int(5);
float *ptrFloat = new float(7.2);
char *ptrChar = new char('A');
cout << "Size of pointer ptrInt is " << sizeof(ptrInt) << endl;
cout << "Size of *ptrInt is " << sizeof(*ptrInt) << endl;
cout << "Size of pointer ptrFloat is " << sizeof(ptrFloat) << endl;
cout << "Size of *ptrFloat is " << sizeof(*ptrFloat) << endl;
cout << "Size of pointer ptrChar is " << sizeof(ptrChar) << endl;
cout << "Size of *ptrChar is " << sizeof(*ptrChar) << endl;
return 0;
}
Output:
Size of pointer ptrInt is 8
Size of *ptrInt is 4
Size of pointer ptrFloat is 8
Size of *ptrFloat is 4
Size of pointer ptrChar is 8
Size of *ptrChar is 1
Example 7:
This example demonstrates nesting of the sizeof
operator.
#include
using namespace std;
int main()
{
int num = 3;
double rate = 5.5;
cout << "Nesting sizeof operator: sizeof(num * sizeof(rate)) is "
<< sizeof(num * sizeof(rate)) << endl;
return 0;
}
Output:
Nesting sizeof operator: sizeof(num * sizeof(rate)) is 8
Example 8:
This example shows how to determine the size of a structure using sizeof
.
#include
using namespace std;
struct Record
{
int id;
float score;
char name[50];
} record;
int main()
{
cout << "Size of structure Record is " << sizeof(record) << endl;
return 0;
}
Output:
Size of structure Record is 56