In C Language, Operators are symbols that perform operations on operands.
Operators enable us to manipulate variables and constants to perform various computations and operations.
C supports a wide range of operators, which can be classified into several categories:
Used to perform arithmetic operations such as addition, subtraction, multiplication, division, and modulus.
+ (addition): The `+` operator is used to add two values together.
- (subtraction): The `-` operator is used to subtract one value from another.
* (multiplication): The `*` operator is used to multiply two values.
/ (division): The `/` operator divides one value by another.
% (modulus): The `%` operator returns the remainder of the division of one value by another.
Used to compare two values and determine the relationship between them. Relational operators return a boolean value (1 for true, 0 for false).
== (equal to)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
Used to perform logical operations on boolean expressions. Logical operators return a boolean value.
&& (logical AND)
|| (logical OR)
! (logical NOT)
Used to assign values to variables. The assignment operator (=) is the most common, but C also provides compound assignment operators.
= (simple assignment): The simple assignment operator `=` assigns the value on the right-hand side to the variable on the left-hand side.
+= (addition assignment): The addition assignment operator `+=` adds the value on the right-hand side to the variable on the left-hand side.
-= (subtraction assignment): The subtraction assignment operator `-=` subtracts the value on the right-hand side to the variable on the left-hand side.
*= (multiplication assignment): The multiplication assignment operator `*=` multiples the value on the right-hand side to the variable on the left-hand side.
/= (division assignment): The division assignment operator `/=` divides the value on the right-hand side to the variable on the left-hand side.
%= (modulus assignment): The modulus assignment operator `%=` performs the modulus operation on the variable on the left-hand side with the value on the right-hand side.
Used to increment (++) or decrement (--) the value of a variable.
++ (increment): The increment `++` operators are used to increase the value of a variable by 1.
-- (decrement): The decrement `--` operators are used to decrease the value of a variable by 1.
Provides a compact way to write simple conditional expressions.
condition ? expression1 : expression2
If the condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.
Used to separate multiple expressions in a statement. The value of the entire expression is the value of the rightmost expression.
expr1, expr2
Used to determine the size of a data type or variable in bytes.
"sizeof(int)" returns the size of an integer in bytes.
#include <stdio.h> int main() { int itemInCart = 10; printf("%d\n", sizeof(itemInCart));// Output: 4 return 0; }
These are the fundamental operators in C programming. Understanding how to use and combine these operators is crucial for writing effective and efficient C code.