Operators in C Tutorials
Programming
Operators in C
Operators are special symbols used to perform operations on variables and values. These operations can be mathematical calculations, comparisons, logical checks, bit-level manipulations, and more.
The values or variables on which operators perform operations are called operands.
For example:
#include <stdio.h>
int main()
{
int sum = 10 + 20;
printf("%d", sum);
return 0;
}Output
30In the above example, + is an operator and 10 and 20 are operands.
Unary, Binary, and Ternary Operators
Operators can be classified based on the number of operands they use.
Unary Operators
Unary operators work on a single operand.
Examples:
Increment (
++)Decrement (
--)Logical NOT (
!)
Example:
int a = 10;
a++;Binary Operators
Binary operators work on two operands.
Examples:
Addition (
+)Subtraction (
-)Multiplication (
*)Division (
/)
Example:
int sum = 10 + 20;Ternary Operators
Ternary operators work on three operands.
The conditional operator (?:) is the only ternary operator in C.
Example:
int result = (10 > 5) ? 1 : 0;Types of Operators in C
C provides several types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
The most commonly used arithmetic operators are:
+Addition-Subtraction*Multiplication/Division%Modulus (Remainder)++Increment--Decrement
Example
#include <stdio.h>
int main()
{
int a = 25, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}Output
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0Relational Operators
Relational operators are used to compare two values.
These operators always return:
1(True)0(False)
Relational operators include:
<Less Than>Greater Than<=Less Than or Equal To>=Greater Than or Equal To==Equal To!=Not Equal To
Example
#include <stdio.h>
int main()
{
int a = 25, b = 5;
printf("a > b = %d\n", a > b);
printf("a < b = %d\n", a < b);
printf("a == b = %d\n", a == b);
return 0;
}Output
a > b = 1
a < b = 0
a == b = 0Logical Operators
Logical operators are used to combine multiple conditions.
The logical operators in C are:
Logical AND (&&)
Returns true only when both conditions are true.
a && bLogical OR (||)
Returns true if at least one condition is true.
a || bLogical NOT (!)
Reverses the result of a condition.
!aExample
#include <stdio.h>
int main()
{
int a = 25, b = 5;
printf("a && b = %d\n", a && b);
printf("a || b = %d\n", a || b);
printf("!a = %d\n", !a);
return 0;
}Output
a && b = 1
a || b = 1
!a = 0Bitwise Operators
Bitwise operators perform operations directly on binary bits.
Bitwise operators include:
&Bitwise AND|Bitwise OR^Bitwise XOR~Bitwise NOT<<Left Shift>>Right Shift
Example
#include <stdio.h>
int main()
{
int a = 25, b = 5;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
return 0;
}Output
a & b = 1
a | b = 29
a ^ b = 28Assignment Operators
Assignment operators are used to assign values to variables.
Simple Assignment Operator
a = b;Compound Assignment Operators
a += b;
a -= b;
a *= b;
a /= b;
a %= b;These operators perform an operation and assignment in a single step.
Example
#include <stdio.h>
int main()
{
int a = 10;
a += 5;
printf("%d\n", a);
return 0;
}Output
15sizeof Operator
The sizeof operator is used to determine the size of a variable or data type in bytes.
Example
#include <stdio.h>
int main()
{
int num = 10;
printf("%zu", sizeof(num));
return 0;
}Output
4Comma Operator (,)
The comma operator allows multiple expressions to be evaluated in a single statement.
Example
int a, b;
a = 10, b = 20;Conditional Operator (?:)
The conditional operator is a shorthand version of the if...else statement.
Syntax
condition ? expression1 : expression2;Example
int result = (10 > 5) ? 100 : 200;Output
100Dot (.) and Arrow (->) Operators
These operators are used with structures.
Dot Operator
Used with a structure variable.
student.nameArrow Operator
Used with a structure pointer.
ptr->nameCast Operator
The cast operator is used to convert one data type into another.
Example
float num = 10.75;
printf("%d", (int)num);Output
10Address-of (&) and Dereference (*) Operators
These operators are commonly used with pointers.
Address-of Operator (&)
Returns the memory address of a variable.
int num = 10;
printf("%p", &num);Dereference Operator (*)
Accesses the value stored at a memory address.
int num = 10;
int *ptr = #
printf("%d", *ptr);Output
10Summary
Operators are special symbols used to perform operations on variables and values.
The values on which operators work are called operands.
Operators are classified as unary, binary, and ternary operators.
Arithmetic operators perform mathematical calculations.
Relational operators compare values and return true or false.
Logical operators combine multiple conditions.
Bitwise operators work directly on binary bits.
Assignment operators assign values to variables.
Special operators such as
sizeof,?:,&,*, and type casting provide additional functionality in C.