C – Operator and Expression8 min read

Operators are the mathematical symbols that are used to perform a mathematical operation on operands. These symbols tell the compiler to perform respective operations. An expression is formed by joining constants and variables in C programming.

Example:

In the above example, 10 and 5 are operands and + is the operation performed between these two operands.

Types of Operator in C:

  • Arithmetic Operator
  • Increment and Decrement Operators
  • Relational Operator
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • sizeof Operator
  • Other Operators

C Arithmetic Operator

These are used to perform mathematical operations on operands such as addition, subtraction, multiplication, division etc.

  • Addition: ‘+’ operator adds two operands. eg, a+b.
  • Subtraction: ‘-‘ operator subtracts two operands. eg, a-b.
  • 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.

Example of Arithmetic Operator

The output of Arithmetic Operator:


C Increment and Decrement Operators

These are the Unary Operators as they operate on a single operand. They are ++ and -- operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.

  • Post-Increment or Post-Decrement:
    First, the value is used for operation and then incremented or decremented. Represented as a++ or a–.
  • Pre-Increment Pre-Decrement:
    Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.

Example of Increment and Decrement Operator

The output of Increment and Decrement Operator.


C Relational Operator

Relational operators are used to compare two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values. They are used with a loop statement and also with if-else statements.

List of relational operators in C:

OperatorNameExample
==Equal to.A == B
!=Not equal.A != B
>Greater than.A > B
<Less than.A < B
>=Greater than or equal to.A >= B
<=Less than or equal to.A <= B

Example of Relational Operator

The output of Relational Operator.


C Assignment Operators

Assignment operator is used to assigning a value to any variable. This operator assigns the value of the right-hand side of an operator to the left-hand side.
These are the short version formed by combining the two operators.

For example Instead of writing, int a = a+3, we can write a += 3.

  • Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
    a = 20; B = 30; Ch = 'cha';
  • 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;
  • Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
    C %= A is equivalent to C = C % A ;

Example of Assignment Operators

The output of Assignment Operators:


C Logical Operators

Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. All of the binary logical operators return two boolean values that are true and false depending upon 0 and 1.

OperatorDescriptionExample
&& (logical and)If both the operands are non-zero, then the condition becomes true.(X && Y) is false
|| (logical or)If any of the two operands are non-zero, then the condition becomes true.(X || Y) is true
! (logical not)Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(X && Y) is true

Example of Logical Operators

The output of Logical Operator.


C Bitwise Operators

In C programming, Bitwise operators are used to perform a bit-level operation. All the mathematical operations are converted to bit-level which makes processing faster and also saves power.

OperatorsName of operators
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Shift left
>>Shift right

Example of Bitwise Operators.

The output of Bitwise operator.


sizeof Operator

sizeof operator is a unary operator that returns the size of constants, variables, array, etc.
Example: sizeof(x), return size of the variable x.

Example of sizeof Operator

The output of sizeof Operator.


Other Operators:

Comma Operator (,)This operator is used to link related operators together. We use a comma operator when we declare many variables in a single line as shown below.
Example: a, c, d = 0, z;
& operatorThis is used to get the address of the variable.
Example: pt = &x
* operatorThis is used as a pointer to a variable.
Example: int *pt;

C Program to demonstrate & and * operator

In this C program, & is used to get the address of a variable and * is used to get the value of variable.

Output: 10


MORE

Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More