What are the different types of operators in C++? Provide examples of each type.
Question
What are the different types of operators in C++? Provide examples of each type.
Solution
C++ has several types of operators. Here are the main ones:
-
Arithmetic Operators: These are used to perform mathematical operations like addition, subtraction, multiplication, etc. Example:
int a = 10, b = 20; int c = a + b; // addition int d = a - b; // subtraction int e = a * b; // multiplication int f = b / a; // division int g = b % a; // modulus
-
Relational Operators: These are used to compare two values. Example:
int a = 10, b = 20; bool c = a == b; // equal to bool d = a != b; // not equal to bool e = a > b; // greater than bool f = a < b; // less than bool g = a >= b; // greater than or equal to bool h = a <= b; // less than or equal to
-
Logical Operators: These are used to combine two or more conditions. Example:
int a = 10, b = 20, c = 30; bool d = (a > b) && (a > c); // logical AND bool e = (a > b) || (a > c); // logical OR bool f = !(a > b); // logical NOT
-
Assignment Operators: These are used to assign values to variables. Example:
int a = 10; // assignment operator a += 20; // addition assignment operator a -= 10; // subtraction assignment operator a *= 2; // multiplication assignment operator a /= 2; // division assignment operator a %= 3; // modulus assignment operator
-
Increment and Decrement Operators: These are used to increase or decrease the value of a variable by 1. Example:
int a = 10; a++; // increment operator a--; // decrement operator
-
Bitwise Operators: These are used to perform bit-level operations. Example:
int a = 10, b = 20; int c = a & b; // bitwise AND int d = a | b; // bitwise OR int e = a ^ b; // bitwise XOR int f = ~a; // bitwise NOT int g = a << 2; // left shift int h = a >> 2; // right shift
-
Ternary Operator: This is a shorthand way of writing an if-else statement. Example:
int a = 10, b = 20; int c = (a > b) ? a : b; // ternary operator
-
Comma Operator: This is used to link related expressions together. Example:
int a = (10, 20, 30); // a becomes 30
-
sizeof Operator: This is used to get the size of a variable or data type. Example:
int a = 10; int size = sizeof(a); // size becomes 4
-
Pointer Operators: These are used to work with pointers. Example:
int a = 10; int *p = &a; // & is the address operator int b = *p; // * is the dereference operator, b becomes 10
-
Type Casting Operators: These are used to convert one data type to another. Example:
int a = 10; double b = (double)a; // type casting operator, b becomes 10.0
Similar Questions
What are the different types of operators in C++? Provide examples of each type.
Which one of the following operators is a unary operator in c language? ans.&<<&&sizeof() Previous Marked for Review Next
Which of the following is NOT a type of Operators in Python?Arithmetic OperatorsLogical OperatorsBitwise OperatorComplex Operators
In C programming, which of the following statements is true about the >>= operator?
What are basic data types supported in the C Programming Language?Each variable in C has an associated data type. Each
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.