Operators in C
What are Operators in C
In C, operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. For example, the addition operator +
adds two numbers and the assignment operator =
assigns a value to a variable.
C has a rich set of operators, including arithmetic operators, relational operators, logical operators, bitwise operators, and more.
Operator, Operands and Result
Operator: An operator is a symbol that performs a specific operation on one or more operands.
Operand: An operand is a value or variable on which the operator performs the operation.
Result: The result of the operation is the value that the operator produces after performing the operation on the operands.
For example, in the expression 5 + 10
, the operator is +
, the operands are 5 and 10
, and the result is 15
.
Types of operators
There are several types of operators that can be used in expressions and statements.
These include:
Arithmetic operators: These operators perform basic mathematical operations such as
addition (+)
,subtraction (-)
,multiplication (*)
,division (/)
, andmodulus (%)
.Comparison operators: These operators compare two values and return a Boolean value (
true
orfalse
) based on the comparison. Examples include equal to(==)
, not equal to(!=)
, greater than(>)
, less than(<)
, greater than or equal to(>=)
, and less than or equal to(<=)
.Logical operators: These operators are used to combine multiple comparisons or Boolean values. Examples include and
(&&)
, or(||)
, and not(!)
.Assignment operators: These operators are used to assign a value to a variable. The most basic assignment operator is the equal sign
(=)
, but there are also compound assignment operators such as+=, -=, *=, and /=
.Ternary operator: The ternary operator is a shorthand for an
if-else
statement. It takes the form ofx ? y : z
, wherex
is a condition,y
is the value returned if the condition is true, andz
is the value returned if the condition is false.Bitwise operator: These operators work on bits and perform bit-by-bit operation. Some examples include
&, |, ^, ~, <<, >>
.Special operator: These operators have special meaning and functionality in C. Examples include
sizeof, &, *, ->, ., ?
:Increment and Decrement operator: These operators are used to increase or decrease the value of a variable by 1. Examples include ++ and --.