C Operators: The Essentials for Efficient and Flexible Programming
C operators are symbols that perform operations on data in your program. They are the essential tools for manipulating variables, performing calculations, and making decisions. Here's a breakdown of the main categories of operators in C:
In the example, related to using the + operator in HTML or JavaScript, here's a simple example:
C Operators
int main() {
int myNum = 100 + 50;
printf("%d", myNum);
return 0;
}
Output
150
While the + operator is commonly utilized to add together two values, as demonstrated in the example above, its versatility extends to combining a variable with a value or pairing two variables. In addition to its basic arithmetic role, the + operator serves as a flexible tool for performing addition operations within the context of variables:
C Operators
int main() {
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
printf("%d\n", sum1);
printf("%d\n", sum2);
printf("%d\n", sum3);
return 0;
}
Output
150
400
800
C divides the operators into the following groups:
Arithmetic operators : Examples: + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
.
Assignment operators : Examples: = (assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign),
etc.
Comparison operators : Examples: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
.
Logical operators : Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).&& (logical AND), || (logical OR), ! (logical NOT)
.
Bitwise operators : Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift)
.
Increment/Decrement Operators: Examples: ++ (pre-increment/post-increment), -- (pre-decrement/post-decrement)
.
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Example
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
-- | Decrement | Decreases the value of a variable by 1 | --x |
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:
C Operators
int main() {
int x = 10;
printf("%d", x);
return 0;
}
Output
10
The addition assignment operator (+=) adds a value to a variable:
C Operators
int main() {
int x = 10;
x += 5;
printf("%d", x);
return 0;
}
Output
15
A list of all assignment operators:
Example
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x+=3 | x=x+3 |
-= | x-=3 | x=x-3 |
*= | x*=3 | x=x/3 |
/= | x%=3 | x=x%3 |
%= | x%=3 | x=x%3 |
&= | x&=3 | x=x&3 |
|= | x|=3 | x=x|3 |
^= | x^=3 | x=x^3 |
>>= | x>>=3 | x=x>>3 |
<<= | x<<=3 | x=x<< 3 |
Comparison Operators in C Programming:
Comparison operators are fundamental building blocks in programming languages like C. They allow you to compare the values of two operands and determine their relative position or equality. Here's a breakdown of the common comparison operators in C:
Basic Operators:
Equal to (==): Checks if two operands have the same value. Returns true (1) if equal, false (0) otherwise.
Not equal to (!=): Checks if two operands have different values. Returns true (1) if not equal, false (0) otherwise.
Greater than (>): Checks if the left operand is greater than the right operand. Returns true (1) if greater, false (0) otherwise.
Less than (<): Checks if the left operand is less than the right operand. Returns true (1) if less, false (0) otherwise.>
C Operators
int main() {
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater than 3
return 0;
}
Output
1
Visit more list of all comparison operators:
Example
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | x == y | Returns 1 if the values are equal |
!= | Not equal | x != y | Returns 1 if the values are not equal |
> | Greater than | x > y | Returns 1 if the first value is greater than the second value |
< | Less than | x < y | Returns 1 if the first value is less than the second value |
>= | Greater than or equal to | x >= y | Returns 1 if the first value is greater than, or equal to, the second value |
<= | Less than or equal to | x <= y | Returns 1 if the first value is less than, or equal to, the second value |
Logical Operators
Logical operators are special symbols used in programming and mathematics to combine two or more logical expressions and return a single logical value (True or False).
Here are the most common logical operators and their definitions: