Operators in C (original) (raw)
In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.
**What is an Operator in C?
A C operator can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called **operands. So, we can say that the operators are the symbols that perform operations on operands.
**For example:
C `
#include <stdio.h> int main() {
// Expression for getting sum
int sum = 10 + 20;
printf("%d", sum);
return 0;
}
`
In the above expression, '+' is the **addition operator that tells the compiler to add both of the operands 10 and 20. To dive deeper into how operators are used with data structures, the **C Programming Course Online with Data Structures covers this topic thoroughly.
Types of Operators in C
C language provides a wide range of built in operators that can be classified into 6 types based on their functionality:
Table of Content
- Arithmetic Operators
- Relational Operators
- Logical Operator
- Bitwise Operators
- Assignment Operators
- Other Operators
**1. Arithmetic Operators
The **arithmetic operators are used to perform arithmetic/mathematical operations on operands. **There are 9 arithmetic operators in C language:
S. No. | Symbol | **Operator | Description | Syntax |
---|---|---|---|---|
1 | ****+** | **Plus | Adds two numeric values. | **a + b |
2 | **- | **Minus | Subtracts right operand from left operand. | **a - b |
3 | *** | **Multiply | Multiply two numeric values. | **a * b |
4 | ****/** | **Divide | Divide two numeric values. | **a / b |
5 | ****%** | **Modulus | Returns the remainder after diving the left operand with the right operand. | **a % b |
6 | ****+** | **Unary Plus | Used to specify the positive values. | ****+a** |
7 | **- | **Unary Minus | Flips the sign of the value. | **-a |
8 | ****++** | **Increment | Increases the value of the operand by 1. | **a++ |
9 | **-- | **Decrement | Decreases the value of the operand by 1. | **a-- |
**Example of C Arithmetic Operators
C `
#include <stdio.h>
int main() {
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
`
Output
a + b = 30 a - b = 20 a * b = 125 a / b = 5 a % b = 0 +a = 25 -a = -25 a++ = 25 a-- = 26
**2. Relational Operators
The **relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.
**These are a total of 6 relational operators in C:
S. No. | Symbol | **Operator | Description | Syntax |
---|---|---|---|---|
1 | ****<** | **Less than | Returns true if the left operand is less than the right operand. Else false | **a < b |
2 | **> | **Greater than | Returns true if the left operand is greater than the right operand. Else false | **a > b |
3 | ****<=** | **Less than or equal to | Returns true if the left operand is less than or equal to the right operand. Else false | **a <= b |
4 | **>= | **Greater than or equal to | Returns true if the left operand is greater than or equal to right operand. Else false | **a >= b |
5 | **== | **Equal to | Returns true if both the operands are equal. | **a == b |
6 | ****!=** | **Not equal to | Returns true if both the operands are NOT equal. | **a != b |
Example of C Relational Operators
C `
#include <stdio.h>
int main() { int a = 25, b = 5;
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}
`
Output
a < b : 0 a > b : 1 a <= b: 0 a >= b: 1 a == b: 0 a != b : 1
Here, 0 means false and 1 means true.
**3. Logical Operator
**Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either **true or **false.
**There are 3 logical operators in C:
S. No. | Symbol | **Operator | Description | Syntax |
---|---|---|---|---|
1 | ****&&** | **Logical AND | Returns true if both the operands are true. | **a && b |
2 | ****| | ** | **Logical OR | Returns true if both or any of the operand is true. |
3 | ****!** | **Logical NOT | Returns true if the operand is false. | ****!a** |
Example of Logical Operators in C
C `
#include <stdio.h>
int main() { int a = 25, b = 5;
// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
`
Output
a && b : 1 a || b : 1 !a: 0
**4. Bitwise Operators
The **Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.
**There are 6 bitwise operators in C:
S. No. | Symbol | **Operator | Description | Syntax |
---|---|---|---|---|
1 | ****&** | **Bitwise AND | Performs bit-by-bit AND operation and returns the result. | **a & b |
2 | ****|** | **Bitwise OR | Performs bit-by-bit OR operation and returns the result. | **a | b |
3 | **^ | **Bitwise XOR | Performs bit-by-bit XOR operation and returns the result. | **a ^ b |
4 | **~ | **Bitwise First Complement | Flips all the set and unset bits on the number. | **~a |
5 | ****<<** | **Bitwise Leftshift | Shifts the number in binary form by one place in the operation and returns the result. | **a << b |
6 | **>> | **Bitwise Rightshilft | Shifts the number in binary form by one place in the operation and returns the result. | **a >> b |
Example of Bitwise Operators
C `
#include <stdio.h>
int main() { int a = 25, b = 5;
// using operators and printing results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);
return 0;
}
`
Output
a & b: 1 a | b: 29 a ^ b: 28 ~a: -26 a >> b: 0 a << b: 800
**5. Assignment Operators
**Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.
**In C, there are 11 assignment operators:
S. No. | Symbol | **Operator | Description | Syntax |
---|---|---|---|---|
1 | **= | **Simple Assignment | Assign the value of the right operand to the left operand. | **a = b |
2 | ****+=** | **Plus and assign | Add the right operand and left operand and assign this value to the left operand. | **a += b |
3 | **-= | **Minus and assign | Subtract the right operand and left operand and assign this value to the left operand. | **a -= b |
4 | ***= | **Multiply and assign | Multiply the right operand and left operand and assign this value to the left operand. | **a *= b |
5 | ****/=** | **Divide and assign | Divide the left operand with the right operand and assign this value to the left operand. | **a /= b |
6 | ****%=** | **Modulus and assign | Assign the remainder in the division of left operand with the right operand to the left operand. | **a %= b |
7 | ****&=** | **AND and assign | Performs bitwise AND and assigns this value to the left operand. | **a &= b |
8 | ****|=** | **OR and assign | Performs bitwise OR and assigns this value to the left operand. | **a |= b |
9 | **^= | **XOR and assign | Performs bitwise XOR and assigns this value to the left operand. | **a ^= b |
10 | **>>= | **Rightshift and assign | Performs bitwise Rightshift and assign this value to the left operand. | **a >>= b |
11 | ****<<=** | **Leftshift and assign | Performs bitwise Leftshift and assign this value to the left operand. | **a <<= b |
Example of C Assignment Operators
C `
#include <stdio.h>
int main() { int a = 25, b = 5;
// using operators and printing results
printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n", a |= b);
printf("a ^= b: %d\n", a ^= b);
printf("a >>= b: %d\n", a >>= b);
printf("a <<= b: %d\n", a <<= b);
return 0;
}
`
Output
a = b: 5 a += b: 10 a -= b: 5 a *= b: 25 a /= b: 5 a %= b: 0 a &= b: 0 a |= b: 5 a ^= b: 0 a >>= b: 0 a <<= b: 0
**6. Other Operators
Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here:
**sizeof Operator
- **sizeof is much used in the C programming language.
- It is a compile-time unary operator which can be used to compute the size of its operand.
- The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
- Basically, the sizeof the operator is used to compute the size of the variable or datatype.
**Syntax
C `
sizeof (operand)
`
**Comma Operator ( , )
The **comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
The comma operator has the lowest precedence of any C operator. It can act as both operator and separator.
**Syntax
C `
operand1 , operand2
`
**Conditional Operator ( ? : )
The **conditional operator is the only ternary operator in C++. It is a conditional operator that we can use in place of if..else statements.
**Syntax
C `
expression1 ? Expression2 : Expression3;
`
Here, **Expression1 is the condition to be evaluated. If the condition(**Expression1) is _**True_then we will execute and return the result of **Expression2 otherwise if the condition(**Expression1) is _**false_then we will execute and return the result of **Expression3.
**dot (.) and arrow (->) Operators
Member operators are used to reference individual members of classes, structures, and unions.
- The **dot operator is applied to the actual object.
- The **arrow operator is used with a pointer to an object.
**Syntax
C `
structure_variable . member; structure_pointer -> member;
`
**Cast Operators
**Casting operators convert one data type to another. For example, int(2.2000) would return 2.
- A cast is a special operator that forces one data type to be converted into another.
**Syntax
C `
(new_type) operand;
`
**addressof (&) and Dereference (*) Operators
**Addressof operator & returns the address o**f a variable and the **dereference operator * is a pointer to a variable. For example *var; will pointer to a variable var.
Example of Other C Operators
C `
// C Program to demonstrate the use of Misc operators #include <stdio.h>
int main() { // integer variable int num = 10; int* add_of_num = #
printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);
return 0;
}
`
Output
sizeof(num) = 4 bytes &num = 0x7ffdb58c037c *add_of_num = 10 (10 < 5) ? 10 : 20 = 20 (float)num = 10.000000
Unary, Binary and Ternary Operators
Operators can also be classified into three types on the basis of the number of operands they work on:
- **Unary Operators: Operators that work on single operand.
- **Binary Operators: Operators that work on two operands.
- **Ternary Operators: Operators that work on three operands.
**Operator Precedence and Associativity
In C, it is very common for an expression or statement to have multiple operators and in this expression, there should be a fixed order or priority of operator evaluation to avoid ambiguity.
**Operator Precedence and Associativityis the concept that decides which operator will be evaluated first in the case when there are multiple operators present in an expression.
The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | ****()** | Parentheses (function call) | left-to-right |
**[] | Brackets (array subscript) | left-to-right | |
****.** | Member selection via object name | left-to-right | |
**-> | Member selection via a pointer | left-to-right | |
**a++ , a-- | Postfix increment/decrement (a is a variable) | left-to-right | |
2 | ****++a , --a** | Prefix increment/decrement (a is a variable) | right-to-left |
**+ , - | Unary plus/minus | right-to-left | |
****! , ~** | Logical negation/bitwise complement | right-to-left | |
****(type)** | Cast (convert value to temporary value of type) | right-to-left | |
*** | Dereference | right-to-left | |
****&** | Address (of operand) | right-to-left | |
**sizeof | Determine size in bytes on this implementation | right-to-left | |
3 | *** , / , % | Multiplication/division/modulus | left-to-right |
4 | **+ , - | Addition/subtraction | left-to-right |
5 | ****<< , >>** | Bitwise shift left, Bitwise shift right | left-to-right |
6 | ****< , <=** | Relational less than/less than or equal to | left-to-right |
**> , >= | Relational greater than/greater than or equal to | left-to-right | |
7 | **== , != | Relational is equal to/is not equal to | left-to-right |
8 | ****&** | Bitwise AND | left-to-right |
9 | **^ | Bitwise XOR | left-to-right |
10 | ****|** | Bitwise OR | left-to-right |
11 | ****&&** | Logical AND | left-to-right |
12 | ****| | ** | Logical OR |
13 | ****?:** | Ternary conditional | right-to-left |
14 | **= | Assignment | right-to-left |
****+= , -=** | Addition/subtraction assignment | right-to-left | |
***= , /= | Multiplication/division assignment | right-to-left | |
****%= , &=** | Modulus/bitwise AND assignment | right-to-left | |
**^= , |= | Bitwise exclusive/inclusive OR assignment | right-to-left | |
****<<=, >>=** | Bitwise shift left/right assignment | right-to-left | |
15 | ****,** | expression separator | left-to-right |