Operator In C

Operator In C

In C, the operator is a symbol that represents a specific operation to be performed on one or more operands. There are various types of operators in C, including arithmetic, relational, logical, bitwise, assignment, and conditional operators.

Here is a brief overview of each of these operator types in C:

  1. Arithmetic operators:

In C programming language, arithmetic operators are used to perform mathematical operations on numerical values. Here are the arithmetic operators supported by C:

  1. Addition (+): Adds two operands and produces the sum. Example: int sum = 5 + 3; // sum will be 8
  2. Subtraction (): Subtracts the second operand from the first operand and produces the difference. Example: int difference = 5 – 3; // difference will be 2
  3. Multiplication (*): Multiplies two operands and produces the product. Example: int product = 5 * 3; // product will be 15
  4. Division (/): Divides the first operand by the second operand and produces the quotient. Example: float quotient = 5.0 / 3.0; // quotient will be 1.666667
  5. Modulus (%): Divides the first operand by the second operand and produces the remainder. Example: int remainder = 5 % 3; // remainder will be 2
  6. Increment (++): Adds one to the operand. Example: int i = 5; i++; // i will be 6
  7. Decrement (): Subtracts one from the operand. Example: int i = 5; i–; // i will be 4

These arithmetic operators can be used with variables, constants, and expressions to perform mathematical operations.Top of Form

2.Relational operators:

In C programming language, relational operators are used to compare two values and produce a result of either true or false. The result of a relational operation is a Boolean value (1 or 0). Here are the relational operators supported by C:

  1. Equal to (==): Checks if two values are equal. Example: int a = 5, b = 5; if (a == b) printf(“a and b are equal”);
  2. Not equal to (!=): Checks if two values are not equal. Example: int a = 5, b = 6; if (a != b) printf(“a and b are not equal”);
  3. Greater than (>): Checks if the first value is greater than the second value. Example: int a = 6, b = 5; if (a > b) printf(“a is greater than b”);
  4. Less than (<): Checks if the first value is less than the second value. Example: int a = 5, b = 6; if (a < b) printf(“a is less than b”);
  5. Greater than or equal to (>=): Checks if the first value is greater than or equal to the second value. Example: int a = 6, b = 5; if (a >= b) printf(“a is greater than or equal to b”);
  6. Less than or equal to (<=): Checks if the first value is less than or equal to the second value. Example: int a = 5, b = 6; if (a <= b) printf(“a is less than or equal to b”);

These relational operators are often used in conditional statements, such as if statements and while loops, to control the flow of the program based on the outcome of the comparison

3. Logical operators

In C programming language, logical operators are used to combine multiple conditions and produce a result of either true or false. The result of a logical operation is a Boolean value (1 or 0). Here are the logical operators supported by C:

  1. Logical AND (&&): Checks if both conditions are true. Example:

 int a   = 5, b = 6; if (a > 0 && b > 0) printf(“Both a and b are greater than 0”);

   2.Logical OR (||): Checks if either of the conditions are true.                   Example:        int a = 5, b = -1; if (a > 0 || b > 0) printf(“At least one of a and b is greater than 0”);

   3.Logical NOT (!): Negates the condition. Example: int a = 5, b = 6; if (!(a > b)) printf(“a is not greater than b”);

These logical operators are often used in conditional statements, such as if statements and while loops, to combine multiple conditions and control the flow of the program based on the outcome of the logical operation.

 

4. Bitwise operators:

In C programming language, bitwise operators are used to manipulate individual bits of a binary value. Bitwise operators are applied to the binary representation of an integer value. Here are the bitwise operators supported by C:

1.Bitwise AND (&): Performs a bitwise AND operation between two values, resulting in a value where each bit is 1 only if both corresponding bits are 1. Example: int a = 5, b = 6; int c = a & b; // c will be 4

2.Bitwise OR (|): Performs a bitwise OR operation between two values, resulting in a value where each bit is 1 if either corresponding bit is 1. Example: int a = 5, b = 6; int c = a | b; // c will be 7

3.Bitwise XOR (^): Performs a bitwise XOR operation between two values, resulting in a value where each bit is 1 if the corresponding bits are different. Example: int a = 5, b = 6; int c = a ^ b; // c will be 3

4.Bitwise NOT (~): Performs a bitwise NOT operation on a value, resulting in a value where each bit is flipped (i.e., 0s become 1s and 1s become 0s). Example: int a = 5; int b = ~a; // b will be -6

     5.Left shift (<<): Shifts the bits of a value to the left by a specified                          number of positions. The leftmost bits are filled with 0s. Example: int a = 5; int b = a << 2; // b will be 20

     6.Right shift (>>): Shifts the bits of a value to the right by a specified number of positions. The rightmost bits are filled with 0s. Example: a >> 1; // b will be 2 int a = 5; int b =2

These bitwise operators can be used to perform low-level operations such as setting and clearing individual bits, or to optimize certain algorithms.

5. Assignment operators:

In C programming language, the assignment operator is the equal sign (=). It is used to assign a value to a variable. The general syntax for using the assignment operator in C is:

variable = expression;

Here, the variable is the name of the variable to which you want to assign a value, and the expression is the value that you want to assign to the variable.

 

Conditional operator:

The ternary operator or conditional operator can be useful in simplifying code and making it more concise. However, it should be used with caution, as overly complex or nested ternary expressions can make the code difficult to read and understand.

The ternary operator in C is a shorthand way of writing an “if-else” statement. It is also known as the conditional operator and has the following syntax:

				
					(condition) ? expression1 : expression2;
				
			

In this syntax, “condition” is a Boolean expression that evaluates to either true or false. If the condition is true, then “expression1” is evaluated and its result is returned. If the condition is false, then “expression2” is evaluated and its result is returned.

Here’s an example to illustrate the use of the ternary operator in C:

				
					int x = 10;
int y = (x > 5) ? 1 : 0;

				
			

In this code, the condition is “x > 5”, which evaluates to true because x is 10. Therefore, the expression “1” is returned and assigned to the variable “y”. If the condition had evaluated to false, then the expression “0” would have been returned and assigned to “y”.

Operators Precedence in C

Here is an operator precedence table in C, showing the precedence levels from highest to lowest:

Precedence

Operator

Description

1

() [] -> .

Parentheses, array subscripting, structure and union member access

2

++ —

Postfix increment and decrement

3

++ — + – ! ~ (type) * & sizeof

Prefix increment and decrement, unary plus and minus, logical and bitwise NOT, cast, dereference and address-of, and size of

4

* / %

Multiplication, division, and modulus

5

+ –

Addition and subtraction

6

<< >>

Bitwise left shift and right shift

7

< <= > >=

Relational operators

8

== !=

Equality and inequality operators

9

&

Bitwise AND

10

^

Bitwise XOR

11

|

Bitwise OR

12

&&

Logical AND

13

||

Logical OR

14

?:

Ternary conditional operator

15

= += -= *= /= %= &= |= ^= <<= >>=

Assignment operators

16

,

Comma operator (used to separate expressions)

It’s important to note that the order of evaluation can be changed by using parentheses to group expressions.

 

Join To Get Our Newsletter
Spread the love