C tokens o Keywords o Constants o Strings o Identifiers and variables o Operators | Bkhakt Kavi Narsinh Mehta University Junagadh | BKNMU

 

In C and many other programming languages, operators are symbols or keywords that represent operations to be performed on data. These operations can include arithmetic calculations, comparisons, logical operations, and more. The operators in C can be categorized into several groups based on their functionality and precedence. Here's an overview of C operators and their hierarchy:


1. **Arithmetic Operators:**

   - Arithmetic operators are used for basic mathematical calculations.

   - Examples: `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulus or remainder).


2. **Relational Operators:**

   - Relational operators are used to compare values and return a Boolean result.

   - Examples: `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal to), `>=` (greater than or equal to).


3. **Logical Operators:**

   - Logical operators are used to perform logical operations on Boolean values.

   - Examples: `&&` (logical AND), `||` (logical OR), `!` (logical NOT).


4. **Assignment Operators:**

   - Assignment operators are used to assign values to variables.

   - Examples: `=` (assignment), `+=` (add and assign), `-=` (subtract and assign), `*=` (multiply and assign), `/=` (divide and assign), `%=` (modulus and assign), and more.


5. **Increment and Decrement Operators:**

   - Increment and decrement operators are used to increase or decrease the value of a variable by one.

   - Examples: `++` (increment), `--` (decrement).


6. **Bitwise Operators:**

   - Bitwise operators perform operations at the bit level.

   - Examples: `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `~` (bitwise NOT), `<<` (left shift), `>>` (right shift).


7. **Conditional (Ternary) Operator:**

   - The conditional operator `? :` is a shorthand way of writing an `if-else` statement.

   - Example: `condition ? true_expression : false_expression`


8. **Comma Operator:**

   - The comma operator `,` is used to separate expressions and evaluates them from left to right.

   - Example: `expression1, expression2, expression3`


9. **Member Access Operators:**

   - These operators are used to access members of structures and unions.

   - Examples: `.` (dot operator), `->` (arrow operator).


10. **Pointer Operators:**

    - These operators are used to work with pointers.

    - Examples: `*` (dereference operator), `&` (address-of operator).


11. **sizeof Operator:**

    - The `sizeof` operator returns the size (in bytes) of a data type or an expression.

    - Example: `sizeof(int)`


12. **Cast Operator:**

    - The cast operator `(type)` is used to explicitly convert a value from one data type to another.

    - Example: `(float) 42`


The hierarchy of operators in C determines the order in which operators are evaluated when expressions have multiple operators. Here's a general hierarchy from highest to lowest precedence:


1. **Parentheses `( )`:** Expressions inside parentheses are evaluated first.

2. **Unary Operators:** Unary operators like `++`, `--`, `+`, `-`, and `!` are evaluated next.

3. **Multiplication and Division (`*`, `/`, `%`):** These operators have higher precedence than addition and subtraction.

4. **Addition and Subtraction (`+`, `-`):** Addition and subtraction operators have lower precedence than multiplication and division.

5. **Relational Operators (`<`, `<=`, `>`, `>=`):** These operators are used for comparisons.

6. **Equality Operators (`==`, `!=`):** These operators are used for equality comparisons.

7. **Logical AND (`&&`):** Used for logical AND operations.

8. **Logical OR (`||`):** Used for logical OR operations.

9. **Conditional Operator (`? :`):** Also known as the ternary operator.

10. **Assignment Operators (`=`, `+=`, `-=`):** Assignment operators are evaluated last.


It's important to understand operator precedence when writing complex expressions to ensure that your code behaves as expected. Parentheses can be used to override the default precedence and specify the order of evaluation.

Post a Comment

Thanks for comment.

Previous Post Next Post