Javatpoint Logo
Javatpoint Logo

C Operators

An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of operations in C language.

  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

Precedence of Operators in C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.

Let's understand the precedence by the example given below:

The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).

The precedence and associativity of C operators is given below:

Category OperatorAssociativity
Postfix() [] ->. ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / %Left to right
Additive+ - Left to right
Shift << >> Left to right
Relational< <= > >= Left to right
Equality == != Left to right
Bitwise AND& Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND&& Left to right
Logical OR || Left to right
Conditional?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma , Left to right

Arithmetic Operators:

Arithmetic operators carry out fundamental mathematical operations. The arithmetic operators in C are as follows:

Addition Operator (+): The addition operator adds two operands together.

Syntax:

It has the following syntax:

Example:

Output:

result = 8

Subtraction Operator (-): The second operand is subtracted from the first operand via the subtraction operator.

Syntax:

It has the following syntax:

Example:

Output:

result = 5

Multiplication Operator (*): This operator is used to multiply the two operands.

Syntax:

It has the following syntax:

Example:

Output:

result = 20

Division Operator (/): The first operand and the second operand are divided using the division operator.

Syntax:

It has the following syntax:

Example:

Output:

result = 5

Modulus Operator (%): The modulus operator determines the remainder of the division between two operands.

Syntax:

It has the following syntax:

Example:

Output:

result = 1

Relational Operators:

Relational operators assess the relationship between values by comparing them. They return either true (1) or false (0). The relational operators in C are as follows:

Equality Operator (==): If two operands are equal, the equality operator verifies this.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Inequality Operator (!=): The inequality operator determines whether two operands are equal or not.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Greater than Operator (>): The greater than operator determines if the first operand exceeds the second operand.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Less than Operator (<): The less-than operator determines if the first operand less is than the second operand.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Greater than or Equal to Operator (>=): The greater than or equal to operator determines if the first operand is more than or equal to the second operand.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Less than or Equal To Operator (<=): The less than or equal to operator determines if the first operand must be less than or equal to the second operand.

Syntax:

It has the following syntax:

Example:

Output:

result=1 (true)

Shift Operators:

A binary number's bits can be moved to the left or right using shift operators. The C shift workers are listed below:

Left Shift Operator (<<): The left shift operator moves the bits of the first operand to the left by the number of places indicated by the second argument.

Syntax:

It has the following syntax:

Example:

Output:

result = 20  // 0001 0100 in binary 

Right Shift Operator (>>): The right shift operator shifts the bits of the first operand to the right by the number of positions specified by the second operand.

Syntax:

It has the following syntax:

Example:

Output:

result = 5  // 0000 0101 in binary 

Logical Operators:

Logical operators perform logical operations on boolean values and return either true (1) or false (0). Here are the logical operators in C:

Logical AND Operator (&&): The logical AND operator returns true if both operands are true.

Syntax:

It has the following syntax:

Example:

Output:

result = 1 (true) 

Logical OR Operator (||): The logical OR operator returns true if at least one of the operands is true.

Syntax:

It has the following syntax:

Example:

Output:

result = 1 (true) 

Logical NOT Operator (!): The logical NOT operator negates the value of the operand.

Syntax:

It has the following syntax:

Example:

Output:

result = 0 (false) 

Bitwise Operators:

Bitwise operators perform operations on individual bits of the operands. Here are the bitwise operators in C:

Bitwise AND Operator (&): The bitwise AND operator performs a bitwise AND operation on the corresponding bits of the operands.

Syntax:

It has the following syntax:

Example:

Output:

result = 1  // 0000 0001 in binary 

Bitwise OR Operator (|): The bitwise OR operator performs a bitwise OR operation on the corresponding bits of the operands.

Syntax:

It has the following syntax:

Example:

Output:

result = 7  // 0000 0111 in binary 

Bitwise XOR Operator (^): The bitwise XOR operator performs a bitwise exclusive OR operation on the corresponding bits of the operands.

Syntax:

It has the following syntax:

Example:

Output:

result = 6  // 0000 0110 in binary 

Bitwise NOT Operator (~): The bitwise NOT operator flips each bit of the operand.

Syntax:

It has the following syntax:

Example:

Output:

result = -6  // 1111 1001 in binary (assuming 8-bit representation) 

Ternary or Conditional Operator: The ternary or conditional operator allows you to assign a value based on a condition.

Syntax:

It has the following syntax:

Example:

Output:

result = 5 

Assignment Operator:

Assignment operators are used to assign values to variables. Here is some of the assignment operator in C:

Simple Assignment Operator (=): The simple assignment operator assigns the value from the right side operands to the left side operands.

Syntax:

It has the following syntax:

Example:

Output:

No output. The value 5 is assigned to variable 'a'.

Miscellaneous Operator:

The sizeof operator and the comma operator fall under the miscellaneous operator category.

sizeof Operator: The sizeof operator returns the size, in bytes, of a variable or a data type.

Syntax:

It has the following syntax:

Example:

Output:

size = 4  // Assuming int occupies 4 bytes

Comma Operator (,): The comma operator evaluates multiple expressions and returns the value of the last expression.

Syntax:

It has the following syntax:

Example:

Output:

result = 15  // a = 7, b = 6, a + b = 13

Uses of Operators:

The following are some common uses for the various kinds of operators in C:

  • Calculations in fundamental mathematics are performed using the addition and subtraction operators (+ and -).
  • If the user wants to do some multiplication and division operations, utilize the multiplication and division operators (* and /).
  • The remainder of a division operation is obtained using the modulus operator (%).
  • Equality and inequality operators (== and!=) are needed to compare values and determine whether they are equal or not.
  • Use the greater than and less than operators (>and <) to compare values and determine if one value is larger than or less than
  • A value's relationship to another value may be determined using the larger than or equal to and less than or equal to operators (>= and <=).
  • A binary number's bits are shifted to the left using the left shift operator (<<).
  • A binary number's bits can be shifted to the right using the right shift operator (>>).
  • Use the logical AND operator (&&) to combine many criteria and determine if each condition is true.
  • When combining several criteria, the logical OR operator (||) is used to determine if at least one of the conditions is true.
  • The logical NOT operator (!) is used to negate a condition's value.
  • When two numbers' individual bits are involved, the bitwise AND operator (&) is utilized to accomplish the action.
  • The bitwise OR operator (|) is employed when two numbers' individual bits are involved.
  • Bitwise exclusive OR operator is performed on individual bits of two integers using the bitwise XOR operator ().
  • Use the bitwise NOT operator () to flip or invert the bits of an integer.
  • Use the ternary operator (?:) to assign a value depending on a condition in a compact form.
  • A value is assigned to a variable using the simple assignment operator (=).
  • The sizeof operator is used to calculate a variable's or data type's size in bytes.
  • When evaluating several expressions, the comma operator (,) returns the result of the last expression that was evaluated.

Conclusion:

In this blog, we have covered the several types of C operators, such as arithmetic, relational, shift, logical, bitwise, ternary, assignment, and other operators. We also covered their features, syntax, samples, and what results to expect. By becoming proficient with these operators, you may effectively handle data and write reliable C programs.


Next TopicComments in C



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA