How to install C Language

In the C programming language, operators are symbols that represent specific actions to be performed on data. They allow you to manipulate and process different types of data, such as numbers, characters, and logical values. Operators are essential for performing various computations and comparisons within C programs. In this tutorial, we will explore the concept of operators and discuss the different types of operators available in C. Let's explore the precedence and associativity of some common C operators:

Precedence of Operators

Precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. Here's a table showing the precedence of some common C operators, from highest to lowest:

PrecedenceOperatorDescription
Highest() [] -> .Parentheses, Subscript, Member Selection
++ --Increment, Decrement
+ -Unary Plus, Unary Minus
! ~Logical NOT, Bitwise NOT
* / %Multiplication, Division, Modulo
+ -Addition, Subtraction
<< >>Bitwise Left Shift, Right Shift
< <= > >=Relational Operators
== !=Equality Operators
&Bitwise AND
^Bitwise XOR
|Bitwise OR
&&Logical AND
||Logical OR
?:Conditional Operator
= += -= *= /= %= <<= >>=Assignment Operators
Lowest,Comma Operator

Associativity of Operators

Associativity defines the order in which operators of the same precedence are evaluated. Operators can be left-associative or right-associative. Left-associative operators are evaluated from left to right, while right-associative operators are evaluated from right to left. Here's a table showing the associativity of some common C operators:

AssociativityOperators
Left++ -- + - ! ~
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
Right?:
Right= += -= *= /= %=
<<= >>= &= ^= |=
,

Understanding the precedence and associativity of operators is important when evaluating complex expressions. Parentheses can be used to override the default precedence and explicitly control the order of evaluation.

Example:

In this example, the expression a + b * c is evaluated. According to operator precedence, multiplication (*) has higher precedence than addition (+). So, the expression is evaluated as a + (b * c), resulting in the value 155.

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

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Shift Operators
  • Ternary or Conditional Operators

Assignment Operator

OperatorDescriptionExample
= (Simple Assignment)Assigns the value of the right operand to the left operand.int x = 5;
+= (Add and Assign)Adds the value of the right operand to the left operand and assigns the result to the left operand.x += 3; (equivalent to x = x + 3;)
-= (Subtract and Assign)Subtracts the value of the right operand from the left operand and assigns the result to the left operand.x -= 2; (equivalent to x = x - 2;)
*= (Multiply and Assign)Multiplies the value of the right operand with the left operand and assigns the result to the left operand.x *= 4; (equivalent to x = x * 4;)
/= (Divide and Assign)Divides the value of the left operand by the right operand and assigns the result to the left operand.x /= 2; (equivalent to x = x / 2;)
%= (Modulus and Assign)Computes the modulus of the left operand with the right operand and assigns the result to the left operand.x %= 3; (equivalent to x = x % 3;)

Here's an example to illustrate the usage of assignment operators in C:

Output:

In the example above, we declare two integer variables a and b and perform various arithmetic operations using the corresponding operators. The results are then printed using printf().

Assignment Operators

OperatorDescriptionExample
= (Simple Assignment)Assigns the value of the right operand to the left operand.int x = 5;
+= (Add and Assign)Adds the value of the right operand to the left operand and assigns the result to the left operand.x += 3; (equivalent to x = x + 3;)
-= (Subtract and Assign)Subtracts the value of the right operand from the left operand and assigns the result to the left operand.x -= 2; (equivalent to x = x - 2;)
*= (Multiply and Assign)Multiplies the value of the right operand with the left operand and assigns the result to the left operand.x *= 4; (equivalent to x = x * 4;)
/= (Divide and Assign)Divides the value of the left operand by the right operand and assigns the result to the left operand.x /= 2; (equivalent to x = x / 2;)
%= (Modulus and Assign)Computes the modulus of the left operand with the right operand and assigns the result to the left operand.x %= 3; (equivalent to x = x % 3;)

Here's an example to illustrate the usage of assignment operators in C:

Output:

In the example above, we declare an integer variable x and perform various assignment operations using the corresponding operators. The updated value of x is then printed using printf().

Comparison Operators

OperatorDescriptionExample
== (Equal to)Checks if the operands are equal. Returns 1 if true, 0 if false.int result = (a == b);
!= (Not equal to)Checks if the operands are not equal. Returns 1 if true, 0 if false.int result = (a != b);
> (Greater than)Checks if the left operand is greater than the right operand. Returns 1 if true, 0 if false.int result = (a > b);
< (Less than)Checks if the left operand is less than the right operand. Returns 1 if true, 0 if false.int result = (a < b);
>= (Greater than or equal to)Checks if the left operand is greater than or equal to the right operand. Returns 1 if true, 0 if false.int result = (a >= b);
<= (Less than or equal to)Checks if the left operand is less than or equal to the right operand. Returns 1 if true, 0 if false.int result = (a <= b);

Here's an example to illustrate the usage of comparison operators in C:

Output:

In the example above, we declare two integer variables a and b and perform various comparison operations using the corresponding operators. The results are stored in the result variable and printed using printf().

Logical Operators

OperatorDescriptionExample
&& (Logical AND)Returns true (1) if both operands are true, otherwise false (0).int result = (a && b);
|| (Logical OR)Returns true (1) if either of the operands is true, otherwise false (0).int result = (a || b);
! (Logical NOT)Reverses the logical state of the operand. If the operand is true, it returns false (0), and if the operand is false, it returns true (1).int result = !a;

Here's an example to illustrate the usage of logical operators in C:

Output:

In the example above, we declare two integer variables a and b and perform various logical operations using the corresponding operators. The results are stored in the result variable and printed using printf().

Bitwise Operators

OperatorDescriptionExample
& (Bitwise AND)Performs a bitwise AND operation between the corresponding bits of the operands.int result = a & b;
| (Bitwise OR)Performs a bitwise OR operation between the corresponding bits of the operands.int result = a | b;
^ (Bitwise XOR)Performs a bitwise XOR (exclusive OR) operation between the corresponding bits of the operands.int result = a ^ b;
~ (Bitwise Complement)Flips the bits of the operand, turning 1s into 0s and vice versa.int result = ~a;
<< (Bitwise Left Shift)Shifts the bits of the left operand to the left by a specified number of positions.int result = a << n;
>> (Bitwise Right Shift)Shifts the bits of the left operand to the right by a specified number of positions.int result = a >> n;

Here's an example to illustrate the usage of bitwise operators in C:

Output:

In the example above, we declare two unsigned integer variables a and b and perform various bitwise operations using the corresponding operators. The results are stored in the result variable and printed using printf().

Shift Operators

OperatorDescriptionExample
<< (Left Shift)Shifts the bits of the left operand to the left by a specified number of positions.int result = a << n;
>> (Right Shift)Shifts the bits of the left operand to the right by a specified number of positions.int result = a >> n;

Here's an example to illustrate the usage of shift operators in C:

Output:

In the example above, we declare an unsigned integer variable a and perform shift operations using the left shift (<<) and right shift (>>) operators. The results are stored in the result variable and printed using printf().

Ternary or Conditional Operators

OperatorDescriptionExample
?: (Ternary or Conditional Operator)Selects one of two expressions based on a condition. If the condition is true, the first expression is evaluated; otherwise, the second expression is evaluated.int result = (condition) ? expression1 : expression2;

Here's an example to illustrate the usage of the ternary or conditional operator in C:

Output:

In the example above, we declare two integer variables a and b. The ternary operator is used to determine the maximum and minimum values between a and b. If the condition (a > b) is true, a is assigned to the max variable; otherwise, b is assigned. Similarly, if the condition (a < b) is true, a is assigned to the min variable; otherwise, b is assigned. The results are then printed using printf().