
Operators in C: Overview and Examples
Table of Content:
C provides a rich operator environment. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. Operators are used in the program to manipulate data and variables. They usually form a part of the mathematical or logical expression.
C language operators can be divided into following categories:
Arithmetic Operators
- The basic arithmetic operations in C Programming are addition, subtraction, multiplication, and division.
- Arithmetic Operations are operated on Numeric Data Types as expected.
- Arithmetic Operators are “Binary” Operators i.e they operate on two operands. These operators are used in mathematical expressions in the same way that they are used in algebra.
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds two operands | 5 + 10 =15 |
- (Subtraction) | Subtract second operands from first. Also used to Concatenate two strings | 10 - 5 =5 |
* (Multiplication) | Multiplies values on either side of the operator. | 10 * 5 =50 |
/ (Division) | Divides left-hand operand by right-hand operand. | 10 / 5 =2 |
% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder. | 5 % 2 =1 |
++ (Increment) | Increases the value of operand by 1. | 2++ gives 3 |
-- (Decrement) | Decreases the value of operand by 1. | 3-- gives 2 |
Relational Operators
- Relational Operators are used to checking relation between two variables or numbers.
- Relational Operators are Binary Operators.
- Relational Operators returns “Boolean” value .i.e it will return true or false.
- Most of the relational operators are used in “If statement” and inside Looping statement in order to check truthiness or falseness of condition.
Operators | Descriptions | Examples |
---|---|---|
== (equal to) | This operator checks the value of two operands, if both are equal, then it returns true otherwise false. | (2 == 3) is not true. |
!= (not equal to) | This operator checks the value of two operands, if both are not equal, then it returns true otherwise false. | (4 != 5) is true. |
> (greater than) | This operator checks the value of two operands, if the left side of the operator is greater, then it returns true otherwise false. | (5 > 56) is not true. |
< (less than) | This operator checks the value of two operands if the left side of the operator is less, then it returns true otherwise false. | (2 < 5) is true. |
>= (greater than or equal to) | This operator checks the value of two operands if the left side of the operator is greater or equal, then it returns true otherwise false. | (12 >= 45) is not true. |
<= (less than or equal to) | This operator checks the value of two operands if the left side of the operator is less or equal, then it returns true otherwise false. | (43 <= 43) is true. |
The Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
The following table lists the bitwise operators supported by C.
Assume variable 'R' holds 60 and variable 'S' holds 13, then ?
Operator | Description | Example |
---|---|---|
& (bitwise and) | Bitwise AND operator give true result if both operands are true. otherwise, it gives a false result. | (R & S) will give 12 which is 0000 1100 |
| (bitwise or) | Bitwise OR operator give true result if any of the operands is true. | (R | S) will give 61 which is 0011 1101 |
^ (bitwise XOR) | Bitwise Exclusive-OR Operator returns a true result if both the operands are different. otherwise, it returns a false result. | (R ^ S) will give 49 which is 0011 0001 |
~ (bitwise compliment) | Bitwise One's Complement Operator is unary Operator and it gives the result as an opposite bit. | (~R ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |
<< (left shift) | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | R << 2 will give 240 which is 1111 0000 |
>> (right shift) | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | R >> 2 will give 15 which is 1111 |
Logical Operators
Java supports following 3 logical operator.
Operator | Description | Example |
---|---|---|
&& (logical and) | If both the operands are non-zero, then the condition becomes true. | (0 && 1) is false |
|| (logical or) | If any of the two operands are non-zero, then the condition becomes true. | (0 || 1) is true |
! (logical not) | Logical NOT Operator Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(0 && 1) is true |
The Assignment Operators
Assignment operator supported by Java are as follows
operator | description | example |
---|---|---|
= | assigns values from right side operands to left side operand | A=B |
+= | adds right operand to the left operand and assign the result to left | A+=B is same as A=A+B |
-= | subtracts right operand from the left operand and assign the result to left operand | A-=B is same as A=A-B |
*= | mutiply left operand with the right operand and assign the result to left operand | A*=B is same as A=A*B |
/= | divides left operand with the right operand and assign the result to left operand | A/=B is same as A=A/B |
%= | calculate modulus using two operands and assign the result to left operand | A%=B is same as A=A%B |
<<= | Left shift AND assignment operator. | A <<= 2 is same as A = A<< 2 |
>>= | Right shift AND assignment operator. | A >>= 2 is same as A = A >> 2 |
&= | Bitwise AND assignment operator. | A &= 2 is same as A = A & 2 |
^= | bitwise exclusive OR and assignment operator. | A ^= 2 is same as A = A ^ 2 |
|= | bitwise inclusive OR and assignment operator. | A |= 2 is same as A = A | 2 |
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator / Ternary Operator
Java includes a one of the most special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ?
. It can seem somewhat confusing at first, but the ?
can be used very effectively once mastered. The ?
has this general form:
Expression1 ? Expression2 : Expression3
Expression ? value if true : value if false
Here, Expression1 can be any expression(a>b) which evaluates to a boolean value (true or false). If Expression1 is true, then expression2 will work; otherwise, expression3 is evaluated. The result of the ?
operation is that of the expression evaluated. Both Expression2 and Expression3 are required to return the same type, which can’t be void.
Example of conditional operator
#includevoid main() { int a, b; a = 20; b = (a == 1) ? 10: 25; printf( "Value of b is : %d\n",b ); b = (a == 20) ? 20: 30; printf( "Value of b is : %d\n",b ); }
Output
Value of b is : 25 Value of b is : 20 Press any key to continue . . .
Examples of Conditional Operator
- Question 1: What are enumerations?
- Question 2: Explain the use of comma operator (,).
- Question 3: Describe the order of precedence with regards to operators in C.
- Question 4: What are enumerated types?
- Question 5: What is enum in C?
- Question 6: What is operator in C?
- Question 7: What are the different types of operator in C?
- Question 8: What is & and * operators in C?
- Question 9: What is the value assigned to the following variables? int X1 = 13/3; int X2 = 13%3;
- Question 10: What value will be assigned to the variable X if a = 10, b = 20, c = 30, d = 40 for the expression X = a/b+c*d-c?
Related Questions
- Assignment 1: Program to find subtraction of two numbers using c programming language
- Assignment 2: Program to find multiplication of two numbers.
- Assignment 3: Program to find division of two numbers.
- Assignment 4: Program to find area and circumference of circle.
- Assignment 5: C Program to Calculate Area of Equilatral Triangle
- Assignment 6: C Program to Calculate Area of Circle
- Assignment 7: C Program to Calculate Area of Rectangle
- Assignment 8: Program to find the simple interest.
- Assignment 9: C Program to Calculate Area of Scalene Triangle
- Assignment 10: C Program to Calculate Area of Right angle Triangle
- Assignment 11: Storage size of short datatype
- Assignment 12: + Arithmetic Operators in C, Addition of two operands.
- Assignment 13: - Arithmetic Operators in C, Subtracts second operand from the first.
- Assignment 14: * Arithmetic Operators in C, Multiplies both operands.
- Assignment 15: / Arithmetic Operators in C, Divides numerator by de-numerator.
- Assignment 16: % Arithmetic Operators in C, Modulus Operator and remainder of after an integer division.
- Assignment 17: a++ Arithmetic Operators in C, post-increment operator increases the integer value by one.
- Assignment 18: a-- Arithmetic Operators in C, post-decrement operator decreases the integer value by one.
- Assignment 19: ++a Arithmetic Operators in C, pre-increment operator increases the integer value by one.
- Assignment 20: --a Arithmetic Operators in C, pre-decrement operator decreases the integer value by one.
- Assignment 21: Arithmetic Operators in C, all in one
- Assignment 22: == Relational Operators in C, Checks if the values of two operands are equal or not. If yes, then the condition becomes true othewise false.
- Assignment 23: != Relational Operators in C, Checks if the values of two operands are equal or not. If values are not equal then the condition becomes true.
- Assignment 24: < Relational Operators in C, Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.
- Assignment 25: > Relational Operators in C, Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.
- Assignment 26: <= Relational Operators in C, Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.
- Assignment 27: >= Relational Operators in C, Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.
- Assignment 28: && Logical Operators in C, Logical AND operator. If both the operands are non-zero, then the condition becomes true.
- Assignment 29: || Logical Operators in C, Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.
- Assignment 30: ! Logical Operators in C,Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
- Assignment 31: Logical Operators in C, all in one
- Assignment 32: Relational Operators in C, all in one
- Assignment 33: Logical Operators in C, all in one example 2
- Assignment 34: Operators Precedence in C
- Assignment 35: Operators Precedence in C
- Assignment 36: Write a program to demonstrate the evaluation of expression according to the precedence rule.