Loading content...
Loading content...
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;
}30In the above example, + is an operator and 10 and 20 are operands.
Operators can be classified based on the number of operands they use.
Unary operators work on a single operand.
Examples:
Increment (++)
Decrement (--)
Logical NOT (!)
Example:
int a = 10;
a++;Binary operators work on two operands.
Examples:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Example:
int sum = 10 + 20;Ternary operators work on three operands.
The conditional operator (?:) is the only ternary operator in C.
Example:
int result = (10 > 5) ? 1 : 0;C provides several types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Arithmetic operators are used to perform mathematical calculations.
The most commonly used arithmetic operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
#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;
}a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0Relational 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
#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;
}a > b = 1
a < b = 0
a == b = 0Logical operators are used to combine multiple conditions.
The logical operators in C are:
Returns true only when both conditions are true.
a && bReturns true if at least one condition is true.
a || bReverses the result of a condition.
!a#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;
}a && b = 1
a || b = 1
!a = 0Bitwise operators perform operations directly on binary bits.
Bitwise operators include:
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
#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;
}a & b = 1
a | b = 29
a ^ b = 28Assignment operators are used to assign values to variables.
a = b;a += b;
a -= b;
a *= b;
a /= b;
a %= b;These operators perform an operation and assignment in a single step.
#include <stdio.h>
int main()
{
int a = 10;
a += 5;
printf("%d\n", a);
return 0;
}15The sizeof operator is used to determine the size of a variable or data type in bytes.
#include <stdio.h>
int main()
{
int num = 10;
printf("%zu", sizeof(num));
return 0;
}4The comma operator allows multiple expressions to be evaluated in a single statement.
int a, b;
a = 10, b = 20;The conditional operator is a shorthand version of the if...else statement.
condition ? expression1 : expression2;int result = (10 > 5) ? 100 : 200;100These operators are used with structures.
Used with a structure variable.
student.nameUsed with a structure pointer.
ptr->nameThe cast operator is used to convert one data type into another.
float num = 10.75;
printf("%d", (int)num);10These operators are commonly used with pointers.
Returns the memory address of a variable.
int num = 10;
printf("%p", &num);Accesses the value stored at a memory address.
int num = 10;
int *ptr = #
printf("%d", *ptr);10Operators 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.