Table of Contents
ToggleIntroduction to C Operators
Operators are special symbols used in C programming to perform different types of operations on values and variables. They help programmers perform calculations, compare values, assign data, make decisions, and perform logical and bit-level operations. C provides several types of operators, including arithmetic, relational, logical, assignment, increment and decrement, conditional, and bitwise operators. Each type has a specific purpose and is widely used when writing C programs.
Understanding C operators is essential for beginners because operators are used in almost every C program. In this article, we will learn the different types of operators in C with simple explanations, examples, and programming solutions.
Definition Operator in C programming :
In C programming, an operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation on one or more values (called operands). An operand is a value or variable on which an operator acts. In simple words: Example:Â a+b ; + is the operator and a and b are operands
Types of operators on the Basic Number of Operands
According to the number of operands required for an operator, the operators are classified into unary, binary,a nd ternary operators
- Unary Operator: Unary operators operate on a single operand to perform actions like increment, decrement, negation, etc. They are commonly used for changing the value or evaluating a condition.
Examples include
++a,--a,-a, and!a Binary Operators (in C): Binary operators work with two operands and perform operations such as arithmetic, comparison, or assignment. They are the most commonly used operators in C programming. Examples include
a + b,a > b, anda = b.Ternary Operator (in C): The ternary operator works with three operands and is used as a shorthand for a
if-elsestatement. Its syntax is:condition ? value_if_true : value_if_false;Example:int max = (a > b) ? a : b;Â
Types of operators on the Basic Utility or functions
1. Arithmetic operators
In C programming, arithmetic operators are used to perform basic mathematical calculations on numeric data types. The primary arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/), along with the modulus operator (%) for obtaining the remainder of the division.
2. Relational Operator:
In C programming, relational operators are used to compare two values or expressions, returning a boolean result (true or false). The primary relational operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators are essential for making decisions and controlling the flow of a program through conditional statements.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
3 Logic operators:
In C programming, logical operators are used to perform logical operations on boolean values, returning true or false based on the evaluation of the expressions. The primary logical operators include AND (&&), OR (||), and NOT (!). These operators are essential for constructing complex conditional statements and controlling the flow of a program.

Â
4. Assignment Operators :
In C programming, an assignment operator is used to assign a value to a variable. The most common assignment operator is=, which sets the variable on the left to the value on the right. For example, int a = 5; assigns the value 5 to the variable a.
5. Increment and Decrement Operators:Â
In C programming, increment and decrement operators are used to increase or decrease the value of a variable by 1. The increment operator is ++ and the decrement operator is --.
- They can be used in prefix form (e.g.,
++a) or postfix form (e.g.,a++). Prefix changes the value before use, while postfix uses the value before changing it.
6. Conditional Operator
In C programming, the conditional operator (also called the ternary operator) is a concise way to perform conditional evaluations. It is the only ternary operator in C, taking three operands, and is often used as a shorthand for simple if-else statements.
Example
#include <stdio.h> int main() { int a = 10, b = 5; int max; // Using conditional operator to find the maximum max = (a > b) ? a : b; printf("Maximum value is: %d\n", max); // Output: Maximum value is: 10 return 0; }
Special OperatorsÂ
C language includes several special operators that perform unique operations beyond basic arithmetic and logic. Here are the most important special operators:
Special Operators in C:
- Comma OperatorÂ
( , ) sizeof Operator- Address-of OperatorÂ
( & ) - Pointer Dereference OperatorÂ
( * ) - Ternary (Conditional) OperatorÂ
( ? : ) - Bitwise Operators:
- Bitwise ANDÂ
( & ) - Bitwise ORÂ
( | ) - Bitwise XORÂ
( ^ ) - Bitwise NOTÂ
( ~ ) - Left ShiftÂ
( << ) - Right ShiftÂ
( >> )
Write a program to read two numbers from the user and determine the larger number using the conditional operator
#include <stdio.h>
int main() {
int num1, num2, larger; // Variable declaration
printf("Enter first number: "); // Prompt for first number
scanf("%d", &num1); // Read first number
printf("Enter second number: "); // Prompt for second number
scanf("%d", &num2); // Read second number
larger = (num1 > num2) ? num1 : num2; // Find larger using ternary operator
printf("The larger number is: %d\n", larger);// Display result
return 0; // Exit program
}
Write a C program to input two numbers and display their sum, difference, product, quotient, and remainder.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
printf("Quotient = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Example Output:Â
Enter two numbers: 20 6
Sum = 26
Difference = 14
Product = 120
Quotient = 3
Remainder = 2
Write a C program to input two numbers and check whether the first number is greater than the second number.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is greater than %d", a, b);
else
printf("%d is not greater than %d", a, b);
return 0;
}
Write a C program to check whether a number is between 10 and 50.
#include <stdio.h>int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n >= 10 && n <= 50)
printf("The number is between 10 and 50.");
else
printf("The number is not between 10 and 50.");
return 0;
}
Write a C program to demonstrate assignment operators.
#include <stdio.h>
int main()
{
int a = 10;
a += 5;
printf("After += : %d\n", a);
a -= 3;
printf("After -= : %d\n", a);
a *= 2;
printf("After *= : %d\n", a);
a /= 4;
printf("After /= : %d\n", a);
return 0;
}
Write a C program to demonstrate increment and decrement operators.
#include <stdio.h>
int main()
{
int a = 10;
printf("Original value = %d\n", a);
a++;
printf("After increment = %d\n", a);
a--;
printf("After decrement = %d\n", a);
return 0;
}
Write a C program to find the greater of two numbers using the conditional operator.
#include <stdio.h>
int main()
{
int a, b, greater;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
greater = (a > b) ? a : b;
printf("Greater number = %d", greater);
return 0;
}
