C++ Operator and Expression

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.

Characteristics of operator are:-

  • The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
  • These C operators join individual constants and variables to form expressions.
  • These C operators join individual constants and variables to form expressions.
  • Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.

TYPES OF C OPERATORS:

Arithmetic operators

These are used to perform arithmetic / mathematical operations on operands.

Arithmetic operatorsDefinition
Multiplication: ‘*’ operator multiplies two operands. eg, a*b.
Division: ’/’operator divides the operand by the second. eg, a/b.
Modulus: ’%’operator returns the remainder when the first operand is divided by the second. For example, a%b.
Addition: ‘+’  operator adds two operands. For example, a+b
Subtraction: ‘-‘  operator subtracts two operands. For example, a-b.

Assignment operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is ‘=’.

Assignment operators Definition Example
Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.a = 20;
B = 30; Ch='char'
Add AND assignment operator: ‘+=’This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.c+=7; a+=b;
Subtract AND assignment operator:
‘-=’
This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.c-=7;
a-=b;
Multiply AND assignment operator: ‘*=’This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.c*=7; a*=b;
Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.c/=7; a/=b;
Left shift AND assignment operator: ‘<<=’This operator is used for Left shift Operation.C <<= 2;
Bitwise AND assignment operator: ‘&=’This operator is used for Bitwise AND Operations. C &= 2;
Bitwise exclusive OR and assignment operator:
‘^=’
This operator is used for Bitwise exclusive OR Operations. C ^= 2;
Bitwise inclusive OR and assignment operator:
‘|=
This operator is used for Bitwise inclusive OR. C |= 2;

Logical operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.

Logical operatorsDefinitionExample
Logical AND: ‘&&’ Operator returns true when both the conditions in consideration are satisfied. Otherwise, it returns false.a && b
returns true when both a and b are true.
Logical OR: ‘||’The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise, it returns false.a || b
returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
Logical NOT: ‘!’The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise, it returns false.!a
returns true if a is false, i.e. when a=0.

Bitwise operators

During computation, mathematical operations: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. Bitwise operators are used in C programming to perform bit-level operations.

Bitwise operators DefinitionExample
Bitwise AND: ‘&’Binary AND Operator copies a bit to the result if it exists in both operands.
Bitwise OR: ‘|’Binary OR Operator copies a bit if it exists in either operand.
Bitwise XOR: ‘^’ Binary XOR Operator copies the bit if it is set in one operand but not both.
Binary Left Shift: ‘<<’The left operands value is moved left by the number of bits specified by the right operand.
Binary Right Shift: ‘>>’ The left operands value is moved right by the number of bits specified by the right operand.

Conditional operators (ternary operators) 

The Conditional Operator in C, also called as Ternary operator is one of the C operators, which are used in the decision-making process. Conditional operators return one value if the condition is true and returns another value if the condition is false.his operator is also called as ternary operator.

Increment/decrement operators

Increment and decrement operators are used to add or subtract 1 from the current value of the operand.

Increment operator++
Decrement operator

Increment and Decrement operators can be prefix or postfix. In the prefix style, the value of the operand is changed before the result of the expression and in the postfix style, the variable is modified after the result.

There are two kinds of increment and decrement operator i.e prefix and postfix.

prefix and postfix increment operator

If the operator is used before the variable i.e ++i then it is called prefix increment operator.

If the operator is used before the variable i.e then i++ it is called postfix increment operator.

prefix and postfix decrement operator

If the operator is used before the variable i.e –i then it is called prefix decrement operator.

If the operator is used before the variable i.e i– then it is called postfix decrement operator.

Special operator 

  • Type cast Operator
  • Reference operator or Address Operater (“&”)
  • Dereference operator (“*”) or Pointer Operator
  • Double Pointer operator (“**”)
  • sizeof operator
  • The comma Operator