C Operators
C operators perform mathematical, logical, and bit-level operations on variables and values. They are symbols that tell the compiler to perform specific mathematical or logical operations.
The different types of operators in C include:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Misc operators
C Arithmetic Operators
C has these basic arithmetic operators to perform common mathematical operations:
| Operator | Name | Description |
|---|---|---|
+ |
Addition | x + y returns the sum of x and y |
- |
Subtraction | x - y returns the difference of x and y |
* |
Multiplication | x * y returns the product of x and y |
/ |
Division | x / y returns the quotient of x and y |
% |
Modulo | x % y returns the integer remainder of x divided by y |
++ |
Increment | x++ returns x + 1 |
-- |
Decrement | x-- returns x - 1 |
C Arithmetic Operators Example
This example demonstrates the usage of arithmetic operators in C:
#include <stdio.h>int main() {int a = 10, b = 3;printf("Addition: %d\n", a + b);printf("Subtraction: %d\n", a - b);printf("Multiplication: %d\n", a * b);printf("Division: %d\n", a / b);printf("Modulo: %d\n", a % b);printf("Increment: %d\n", ++a);printf("Decrement: %d\n", --b);return 0;}
Here is the output:
Addition: 13Subtraction: 7Multiplication: 30Division: 3Modulo: 1Increment: 11Decrement: 2
C Relational Operators
C relational operators compare two values and return true or false:
| Operator | Name | Description |
|---|---|---|
== |
Equal | x == y is true if x and y are equal. |
!= |
Not Equal | x != y is true if x and y are not equal. |
< |
Less Than | x < y is true if x is less than y. |
> |
Greater Than | x > y is true if x is greater than y. |
<= |
Less Than or Equal to | x <= y is true if x is less than or equal to y. |
>= |
Greater Than or Equal to | x >= y is true if x is greater than or equal to y. |
C Relational Operators Example
This example demonstrates the usage of relational operators in C:
#include <stdio.h>int main() {int a = 10, b = 5;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;}
Here is the output:
a == b: 0a != b: 1a > b: 1a < b: 0a >= b: 1a <= b: 0
C Logical Operators
C logical operators operate on Boolean values and return Boolean values:
| Operator | Name | Description |
|---|---|---|
! |
Not | !x is true when x is false and false when x is true. |
&& |
And | x && y is true only if x and y are both true. |
|| |
Or | x || y is true if either x or y is true. |
C Logical Operators Example
This example demonstrates the usage of logical operators in C:
#include <stdio.h>int main() {int a = 1, b = 0;printf("a && b: %d\n", a && b);printf("a || b: %d\n", a || b);printf("!a: %d\n", !a);return 0;}
Here is the output:
a && b: 0a || b: 1!a: 0
C Bitwise Operators
C bitwise operators manipulate individual bits in an integer:
| Operator | Name | Description |
|---|---|---|
& |
And | Result has bit set only where both operands have that bit set. |
| |
Or | Result has bit set if either operand has that bit set. |
^ |
Xor | Result has bit set only if only one operand has that bit set. |
~ |
Not | Takes a single operand and inverts each bit in it. |
<< |
Left shift | a << b means a‘s bits are shifted to the left b spaces. |
>> |
Right shift | a >> b means a‘s bits are shifted to the right b spaces. |
C Bitwise Operators Example
This example demonstrates the usage of bitwise operators in C:
#include <stdio.h>int main() {int a = 5, b = 3; // 5 = 0101, 3 = 0011printf("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 << 1: %d\n", a << 1);printf("a >> 1: %d\n", a >> 1);return 0;}
Here is the output:
a & b: 1a | b: 7a ^ b: 6~a: -6a << 1: 10a >> 1: 2
C Assignment Operators
C assignment operators assign the value on the right to the variable on the left:
| Operator | Name | Description |
|---|---|---|
= |
Assignment | a = 3 assigns value 3 to variable a. |
+= |
Addition Assignment | a += 3 is short for a = a + 3. |
-= |
Subtraction Assignment | a -= 3 is short for a = a - 3. |
*= |
Multiplication Assignment | a *= 3 is short for a = a * 3. |
/= |
Division Assignment | a /= 3 is short for a = a / 3. |
%= |
Modulo Assignment | a %= 3 is short for a = a % 3. |
&= |
And Assignment | a &= 3 is short for a = a & 3. |
|= |
Or Assignment | a |= 3 is short for a = a | 3. |
^= |
Xor Assignment | a ^= 3 is short for a = a ^ 3. |
<<= |
Left Shift Assignment | a <<= 3 is short for a = a << 3. |
>>= |
Right Shift Assignment | a >>= 3 is short for a = a >> 3. |
C Assignment Operators Example
This example demonstrates the usage of assignment operators in C:
#include <stdio.h>int main() {int a = 5;a += 3; // a = a + 3printf("a += 3: %d\n", a);a -= 2;printf("a -= 2: %d\n", a);a *= 4;printf("a *= 4: %d\n", a);a /= 3;printf("a /= 3: %d\n", a);a %= 3;printf("a %%= 3: %d\n", a);return 0;}
Here is the output:
a += 3: 8a -= 2: 6a *= 4: 24a /= 3: 8a %= 3: 2
C Misc Operators
C misc operators don’t fit into a particular category:
| Operator | Name | Description |
|---|---|---|
sizeof() |
Sizeof Operator | sizeof(x) returns the size of variable x. |
& |
Address | &x returns the address of variable x. |
* |
Pointer | *x returns a pointer to variable x. Not to be confused with multiplication. |
?: |
Conditional Operator | expression?x:y returns x when expression is true, y otherwise. |
C Misc Operators Example
This example demonstrates the usage of some of these operators in C:
#include <stdio.h>int main() {int a = 10;int *p = &a;printf("Address of a: %p\n", &a);printf("Value of a using pointer: %d\n", *p);int b = (a > 5) ? 100 : 200;printf("Ternary result: %d\n", b);return 0;}
Here is the output:
Address of a: 0x7ffdcb92c540Value of a using pointer: 10Ternary result: 100
Frequently Asked Questions
1. What is the difference between == and = in C?
=is the assignment operator (e.g.,x = 5; assigns value5tox).==is the equality operator used to compare two values (e.g.,x == ychecks ifxequalsy).
2. What is the difference between C && and & operator?
- C
&&is a logical AND operator: It returnstrueif both conditions aretrue. - C
&is a bitwise AND operator: It performs an AND operation bit-by-bit.
3. Can I use arithmetic operators on characters in C?
Yes. Characters in C are stored as ASCII values, so arithmetic operations like 'A' + 1 are valid and return 'B'.
Operators
- sizeof
- Returns the size in bytes of a data type or variable at compile time.
- Ternary operator
- Provides a concise way to write conditional expressions in C.
- Unary operators
- Perform operations on a single operand.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours