Table of Contents
ToggleC operators are the symbols that are used in C programs to perform mathematical and logical operations. C operators combine variables and constants to create expressions. To form expressions, operators, functions, constants, variables and operators are combined.
In the expression X +Y *20. “+”, ” *” and operators X,Y are variables, 20 is constant, and X +Y *20 is an expression.
In this post we will look into special operators in C. But let’s first see what are the other types of operators does C provide.
Types Of Operators In C
C provides 6 types of built-in operators:
- Arithmetic Operators : This includes +, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement
- Relational Operators : This includes ==, !=, >, <, >= and <=
- Logical Operators : This includes &&, || and !
- Bitwise Operators : This includes &, |, ^, ~, >> and <<
- Assignment Operators : This includes =, +=, -=, *=, etc.
- Special Operators : This includes cast operator, comma, sizeof, reference, dereference, double pointer
Arithmetic Operator In C
Arithmetic operator in C can be divided into two types:
- Unary Operator: This kind of operators needs only one operand like ++ and —
- Binary Operator: This kind of operators requires at-least two operand like +,* etc.
The Arithmetic Operators are described below:
Operator | Description | Example |
Addition | The ‘+’ operator is used to perform addition of two operands | x+y |
Subtraction | The ‘-‘ operator is used to perform subtraction of two operands | x-y |
Multiplication | The ‘*’ operator is used to perform multiplication of two operands | x*y |
Division | The ‘/’ operator is used to divide the first operand by the second | x/y |
Modulus | The ‘%’ operator returns the remainder when first operand is divided by the second | x%y |
Increment | The ‘++’ operator is used to increment number by one. It can be post or pre increment | ++x, x++ |
Decrement | The ‘–‘ operator is used to decrement number by one. It can be post or pre decrement | –x, x– |
// Arithmetic Operator In C #include <stdio.h> int main() { int a = 10,b = 12, c; c = a+b; printf("Addition of a and b = %d \n",c); c = a-b; printf("Subtraction of a and b = %d \n",c); c = a*b; printf("Multiplication of a and b = %d \n",c); c = a/b; printf("Division of a and b = %d \n",c); c = a%b; printf("Remainder when a divided by b = %d \n",c); printf("Value of a after increment = %d \n", ++a); printf("Value of b after decrement = %d \n", --b); return 0; }
Output
Addition of a and b = 22
Subtraction of a and b = -2
Multiplication of a and b = 120
Division of a and b = 0
Remainder when a divided by b = 10
Value of a after increment = 11
Value of b after decrement = 11
Relational Operators In C
Operator | Description | Example |
Equal To == | Checks whether the two given operands are equal or not. If yes, it returns true else false | x==y |
Not Equal To != | Checks whether the two given operands are equal or not. If not, it returns true else false. | x!=y |
Greater Than > | Checks whether the first operand is greater than the second operand or not. | x>y |
Less Than < | Checks whether the first operand is lesser than the second operand. | x<y |
Greater than or equal to >= | Checks whether the first operand is greater than or equal to the second operand. | x>=y |
Less than or equal to <= | Checks whether the first operand is less than or equal to the second operand. | x<=y |
//Relational Operators In C #include<stdio.h> int main() { int x = 20, y = 30; printf("x = %d\n", x); printf("y = %d\n", y); // Check if x is greater than y printf("Is x > y : %d\n", x > y); // Check if x is greater than or equal to y? printf("Is x >= y : %d\n", x >= y); // Check if x is smaller than y? printf(" Is x < y : %d\n", x < y); // Check if x is smaller than or equal to y? printf("Is x <= y : %d\n", x <= y); // Check if x is equal to y? printf("Is x == y : %d\n", x == y); // Check if x is not equal to y? printf("Is x != y : %d\n", x != y); return 0; }
Output
x = 20
y = 30
Is x > y : 0
Is x >= y : 0
Is x < y : 1
Is x <= y : 1
Is x == y : 0
Is x != y : 1
Logical Operators In C
Operator | Description | Example |
Logical AND && | This operator returns true when both the conditions which are compared are true. | a && b |
Logical OR || | This operator returns true when any one of the conditions which are compared are true. | x-y |
Logical NOT ! | The ‘*’ operator multiplies two operands | X*y |
#include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; //This will be true when both conditions are true result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d \n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d \n", result); //This will be true when either one of the condition are true result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d \n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d \n", result); // Negates the output result = !(a == b); printf("!(a == b) is %d \n", result); return 0; }
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a == b) is 0
Bitwise Operators In C
Operator | Description | Example |
bitwise AND & | This operator takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1 | x & y |
bitwise OR | | This operator takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 only if any of the bit is 1 | x | y |
bitwise XOR ^ | This operator takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 only if two bits are different | x ^ y |
Left Shift << | This operator takes two numbers and does left shift of the bits of the first operand where the second operand decides the number of places to be shifted | x<<4 |
Right Shift >> | This operator takes two numbers and does right shift of the bits of the first operand where the second operand decides the number of places to be shifted | x>>4 |
bitwise NOT ~ | This operator takes one number and inverts all bits of it | !x |
Check out examples of bitwise operators here
Assignment Operators In C
Operator | Description | Example |
= | This operator is used to assign the value on the right to the variable on the left | X=5 |
+= | This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. | x+=7 |
-= | This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left. | x-=7 |
*= | This operator first multiplies the current value of the variable on left from the value on the right and then assigns the result to the variable on the left | x*=7 |
/= | This operator first divides the current value of the variable on left from the value on the right and then assigns the result to the variable on the left | x/=7 |
Check out examples of Assignment Operators here
Special Operators In C
Now let’s look into special operators in C. There are 6 special operators in C:
- Comma Operator
- Type Cast Operator
- Reference Operator
- Dereference Operator
- Double Pointer
- SizeOf
Comma Operator
The comma operator is type of special operators in C which evaluates first operand and then discards the result of the same, then the second operand is evaluated and result of same is returned. Comma operator is a binary operator and has the least precedent of all C operators.
For Example:
int val= (10, 30);
In this example 10 is discarded and 30 is assigned to val variable.
int k= (fun1(), fun2())
In this example the fun1() is called first and its return value is discarded and then fun2() is called and its return value is assigned to variable k
// Special Operators In C: Comma #include<stdio.h> int fun1(){ return 10; } int fun2(){ return 20; } #include <stdio.h> int main() { // 30 is assigned to val int val= (10, 30); printf("Value of val is %d\n",val); // fun1() is called (evaluated) first followed by fun2(). // The value returned by fun2() is assigned to k int k= (fun1(), fun2()); printf("Value of k is %d", k); }
Output
Value of val is 30
Value of k is 20
Type Cast Operator
The conversion of one datatype to another is called type conversion. The cast operator is one of the special operators in C that is used to perform explicit type conversion. Type conversion are also done by compiler implicitly. As good good programming practices it is advisable to use explicit type conversion wherever necessary.
// Special Operators In C: Cast Operator #include <stdio.h> int main() { int total = 17, count = 5; double mean; mean = (double) total / count; printf("The mean of numbers is: %f\n", mean ); return 0; }
Output
The mean of numbers is: 3.400000
Reference Operator
Reference operator is one of the special operators in C that returns address of the variable with which this operator is associated with.For eg. &a will return address of variable a.
So, now if we want a pointer to point to a variable let’s say y, then we need to get the address of y and assign to pointer. p= &y;
Dereference operators
Dereference operator is one of the special operators in C that returns the value stored in the variable pointed by the specified pointer. For e.g., if we write “*p”, where p is pointer pointing to variable x, it will return the value of the variable x pointed by the pointer “p”.
Hence, if we want the value of the variable pointed by the pointer “p” to be stored in a variable “z”, then we can do so by: y = *p;
// Special Operators In C : Reference and Dereference #include <stdio.h> int main(){ int* p; // Pointer to hold address int k=10; printf("Address of variable k: %d\n",&k); printf("Value of k :%d\n",k); //Store address to pointer p=&k; printf("Address of Pointer p: %d\n",p); printf("Content of Pointer p: %d\n\n",*p); // On changing content of variable pointer on // deferencing returns updated value k=20; printf("Address of Pointer pt: %d\n",p); printf("Content of Pointer pt: %d\n\n",*p); //Assign Values using dereference operator *p=30; printf("Address of variable: %d\n",&k); printf("Value of variable: %d\n",k); return 0; }
Output
Address of variable k: -862092644
Value of k :10
Address of Pointer p: -862092644
Content of Pointer p: 10
Address of Pointer pt: -862092644
Content of Pointer pt: 20
Address of variable: -862092644
Value of variable: 30
Double Pointer operator
Pointers are used to store the address of other variables of similar datatype. But if you want to store the address of a pointer variable, then you again need a pointer to store it. Thus, when one pointer variable stores the address of another pointer variable, it is known as Pointer to Pointer variable or Double Pointer.
In the expression int **p1; we have used two indirection operator(*
) which stores and points to the address of a pointer variable
// Special Operators In C : Double Pointer operator #include <stdio.h> int main() { int k = 100; // pointer for var int *p1, **p2; // storing address of k in p1 p1 = &k; // Storing address of p1 in p2 p2 = &p1; // Displaying results printf("Value of k = %d\n", k ); printf("Value of k using single pointer = %d\n", *p1 ); printf("Value of k using double pointer = %d\n", **p2); return 0; }
Output
Value of k = 100
Value of k using single pointer = 100
Value of k using double pointer = 100
SizeOf
Sizeof is a special operators in C that is a compile-time unary operator that can be used for computing the operand’s size. Sizeof returns an unsigned integral type result, which is often denoted size_t. Sizeof can be applied any data type, including primitive types like integer, floating-point, and pointer types as well as compound datatypes like Structure, union, etc.
The sizeof() operator can be used in a variety of ways depending on the operand type.
- Operand is a Data Type: Sizeof() returns the memory allocated to the data types int, flot, and char…
- Operand is an expression: If sizeof() is used in conjunction with an expression, it returns the expression’s size.
// Special Operators In C : Size of operator #include <stdio.h> int main() { printf("%lu\n", sizeof(char)); printf("%lu\n", sizeof(int)); int a = 10; double d = 30.21; printf("%ld", sizeof(a + d)); return 0; }
Output
1
4
8
Operators Precedence in C
Operator | Description | Associativity |
() [] . -> | Parentheses Brackets Member selection via object name Member selection via pointer | left-to-right |
++ — + – ! ~ (type) * & sizeof | Unary preincrement/predecrement Unary plus/minus Unary logical negation/bitwise complement Unary cast (change type) Dereference Address Determine size in bytes | right-to-left |
* / % | Multiplication/division/modulus | left-to-right |
+ – | Addition/subtraction | left-to-right |
<< >> | Bitwise shift left, Bitwise shift right | left-to-right |
< <= > >= | Relational less than/less than or equal to Relational greater than/greater than or equal to | left-to-right |
== != | Relational is equal to/is not equal to | left-to-right |
& | Bitwise AND | left-to-right |
^ | Bitwise exclusive OR | left-to-right |
| | Bitwise inclusive OR | left-to-right |
&& | Logical AND | left-to-right |
|| | Logical OR | left-to-right |
?: | Ternary conditional | right-to-left |
= += -= *= /= %= &= ^= |= <<= >>= | Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment | right-to-left |
, | Comma | left-to-right |
Conclusion
In this article we discussed about operators in C and also got into details about special operators in C along with its use cases.
Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out.