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:
In C programming language, arithmetic operators are used to perform mathematical operations on numerical values. Here are the arithmetic operators supported by C:
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:
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:
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.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.