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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …