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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More