C Programming Operators

C Programming Operators are symbols useful to perform mathematical and logical operations. You can use these Operators on individual values or variables.

Types of Operators in C programming

The table below will show you the list of available Programming operators with an example.

List of OperatorsDescription
Arithmetic OperatorsUsed to perform basic mathematical calculations like Addition, Subtraction, Multiplication, Division, and Modulus.
Relational OperatorsThe Relational operators are mostly useful in for loops, while loops, if statement or else if statements. Use these to check the relationship between the two variables. If the relationship is true, it will return 1. Otherwise, it will return a value of 0.
Logical OperatorsUsed to combine two or more conditions and perform the logical operations using Logical AND (&&), OR (||), and NOT (!).
Assignment OperatorsUsed to assign the values to the declared variables. = (Equals) is the most commonly used one to assign the result.
Increment & Decrement OperatorsTo increase or decrease the value by 1. ++ is useful to increase the existing variable value by 1. The decrement operator – – is useful to subtract or decrease the existing value by 1.
Bitwise OperatorsBitwise operators are useful for performing bit-level operations. First, the decimal values will convert into a sequence of bits ( binary values), i.e., 0001, 1011, etc. And this will work on these bits like shifting them right to the left etc.
Conditional OperatorThe Conditional is also known as the Ternary operator. We use them in the decision-making process of true or false. Depending upon the expression result, this operator returns the statement.
Sizeof OperatorIt is most useful in finding the structure size and array size.

C Programming Arithmetic Operators Example

In this operator’s example, we will use two variables to perform various arithmetic operations in this programming Language. Refer to the C tutorial page.

#include<stdio.h>

int main()
{
 int a = 24, b = 4;
 int addition, subtraction, multiplication, division, modulus;
 
 addition = a + b; 
 subtraction = a - b; 
 multiplication = a * b; 
 division = a / b; 
 modulus = a % b; 
 
 printf("Adding of two numbers a, b is : %d\n", addition);
 printf("Subtracting of two numbers a, b is : %d\n", subtraction);
 printf("Multiplying two numbers a, b is : %d\n", multiplication);
 printf("Division of two numbers a, b is : %d\n", division);
 printf("Modulus of two numbers a, b is : %d\n", modulus);

}
C Programming Operators Example

C programming Relational Operators Example

In this example, we will use two variables, a and b, to perform various relational operations in programming. The relational operators are useful in the C for loops, C while loops, C if statements, or C else if statements.

// Relational example
#include <stdio.h>

main()
{
  int a = 35;
  int b = 16;
  
  printf("%d >  %d: %d \n", a,b, a > b);
  printf("%d >= %d: %d \n", a,b, a >= b);
  printf("%d <= %d: %d \n", a,b, a <= b);
  printf("%d <  %d: %d \n", a,b, a < b);
  printf("%d == %d: %d \n", a, b, a == b);
  printf("%d != %d: %d \n", a, b, a != b);

}
35 >   16: 1
35 >= 16: 1
35 <= 16: 0
35 <   16: 0
35 == 16: 0
35 !=  16: 0

Assignment Example

For this example, We use the two variables x and Total. They will show you the working functionality of all the Assignment Operators in C Programming Language.

#include <stdio.h>

int main()
{
 int x = 5;
 int Total = 25;

 printf("%d \n", Total += x );
 printf("%d \n", Total -= x );
 printf("%d \n", Total *= x );
 printf("%d \n", Total /= x );
 printf("%d \n", Total %= x );

 return 0;
}
30
25
125
25
0

Logical operators

The logical operators (Logical AND or Logical OR) are helpful to combine two or more conditions. They evaluate multiple conditions and return true or false. The logical NOT operator is used to reverse the result (for instance, true becomes false).

The following example shows a simple example that displays 1 (true) and 0 (false) as output. However, in real-time, we use it in the if statements.

#include <stdio.h>

int main()
{
int a = 10, b = 20;

printf("%d\n", a > 9 && b > 15);
printf("%d\n", a > 20 || b > 10);
printf("%d\n", !(a > 5));
return 0;
}
1
1
0

Bitwise operators

Bitwise AND, OR, XOR, or complement (~) operators perform the bit operations. Similarly, the left shift and right operators in C programming perform the bit shift operations.

In the following example, we use two values, 12 (1100) and 10 (1010), to demonstrate the bitwise operations.

#include <stdio.h>

int main()
{
int a = 12;
int b = 10;

printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
}
a & b  = 8
a | b  = 14
a ^ b  = 6
~a     = -13
a << 1 = 24
a >> 1 = 6

conditional operator

The conditional operator, also known as the ternary operator, is a shorter version of the if else statement. The following example displays even or Odd based on the given value. Here, 10 means even.

#include <stdio.h>

int main()
{
int n = 10;

printf("%d is %s\n", n, (n % 2 == 0) ? "Even" : "Odd");
}
10 is Even

sizeof

We use the sizeof operator to get the size of an operand.

#include <stdio.h>

int main()
{
int a = 10;
float b = 20.5f;
double c = 3.14;
char d = 'A';

printf("a = %zu bytes\n", sizeof(a));
printf("b = %zu bytes\n", sizeof(b));
printf("c = %zu bytes\n", sizeof(c));
printf("d = %zu bytes\n", sizeof(d));
}
a = 4 bytes
b = 4 bytes
c = 8 bytes
d = 1 bytes