Introduction to C Operators
Introduction to C Operators
In the C programming language, operators are special symbols used to perform different operations on operands. They are the building blocks of any C program and form the basis of any arithmetic computation, logical comparison or assignment operation in C.
Types of Operators in C
C language supports a rich set of operators. They are categorized into several groups:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary or Conditional Operators
Let's have a detailed look at each of these operator types:
Arithmetic Operators
Arithmetic operators are employed for mathematical calculations. They include:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
Relational Operators
Relational operators are used for comparison between two operands. They yield a result of 1 if the comparison is true and 0 if it is false. They include:
- Greater than (>)
- Less than (<)
- Equal to (==)
- Not equal to (!=)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Logical Operators
Logical operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They include:
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
Bitwise Operators
Bitwise operators perform operations on the bit level. They include:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Left shift (<<)
- Right shift (>>)
Assignment Operators
Assignment Operators are used to assign value to a variable. The basic assignment operator is =. C language supports multiple compound assignment operators such as +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=.
Unary Operators
Unary operators are used with only one operand. They include:
- Unary Minus (-)
- Unary Plus (+)
- Increment (++), both pre and post
- Decrement (--), both pre and post
Ternary or Conditional Operators
The conditional operator (?:) also known as the ternary operator is used as a shortcut for the if-else statement. It has three operands and hence the name ternary.
Syntax: condition ? expression1 : expression2
Operator Precedence in C
When an expression contains multiple operators, the order of evaluation depends on the precedence of the operators. Operators with higher precedence are evaluated first. For example, the multiplication operator (*) has higher precedence than the addition operator (+) and therefore will be evaluated first.
In conclusion, understanding how to use operators in C effectively can make your programs more efficient and easier to read. Remember, practice is key, so make sure to write a lot of code to familiarize yourself with using different C operators.