Summary: in this tutorial, you will learn how to use C operators including arithmetic, assignment, relational, bit-wise, increment, logical and ternary operators.
C supports many operators to perform various kinds of operations. With C operators, you can do arithmetic operations, comparing data, modifying variables, combining relationship logically, etc.
C operators operates on one or more operands to produce a value.
- The operators that take one operand are called unary operators.
- The operators that require two operands are called binary operators.
- The operator that accepts three operands is called ternary operator. C has only one ternary operator.
C Arithmetic Operators
C supports almost common arithmetic operators such as +,-,*, / and modulus operator %. The modulus operator (%) returns the remainder of integer division calculation. Note that the modulus operator cannot be applied to a double or float.
C Arithmetic Operators Precedence
The C arithmetic operators have precedence rules that are similar to the rules in math. The + and – operators have the same precedence. The precedence of *, / and % operators are higher than the + and – operators. The C arithmetic operators associate from left to right.
The following program demonstrates the C arithmetic operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /* * File: main.c * Author: zentut.com * Purpose: Demonstrates the C arithmetic operators */ #include <stdio.h> void main() { int x = 10, y = 20; printf("x = %d\n", x); printf("y = %d\n", y); /* demonstrate = operator + */ y = y + x; printf("y = y + x; y = %d\n", y); /* demonstrate - operator */ y = y - 2; printf("y = y - 2; y = %d\n", y); /* demonstrate * operator */ y = y * 5; printf("y = y * 5; y = %d\n", y); /* demonstrate / operator */ y = y / 5; printf("y = y / 5; y = %d\n", y); /* demonstrate modulus operator % */ int remainder = 0; remainder = y % 3; printf("remainder = y %% 3; remainder = %d\n", remainder); return 0; } |
Below is the output of the demo program:
1 2 3 4 5 6 7 | x = 10 y = 20 y = y + x; y = 30 y = y - 2; y = 28 y = y * 5; y = 140 y = y / 5; y = 28 remainder = y % 3; remainder = 1 |
C Assignment Operators
C assignment operators are used to assign the value of a variable or expression to another variable. The syntax of assignment operators is as follows:
1 2 | var = expression; var = var; |
Besides the assignment operator, C also supports another short hand format that works the same assignment operator with an additional operator such as +=, -=, *=, /=, %=.
1 2 | var +=expression; var = var + expression; |
Each assignment operator has a priority. And they are evaluated from right to left based on its priority. The following is the assignment operator and its priority: =, +=, -=, *=, /=, %=.
A simple C program that demonstrates assignment operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> /* a program demonstrates C assignment operator */ void main(){ int x = 10; /* demonstrate = operator */ int y = x; printf("y = %d\n",y); /* demonstrate += operator */ y += 10; printf("y += 10;y = %d\n",y); /* demonstrate -= operator */ y -=5; printf("y -=5;y = %d\n",y); /* demonstrate *= operator */ y *=4; printf("y *=4;y = %d\n",y); /* demonstrate /= operator */ y /=2; printf("y /=2;y = %d\n",y); } |
Here is the output:
1 2 3 4 5 | y = 10 y += 10;y = 20 y -=5;y = 15 y *=4;y = 60 y /=2;y = 30 |
C Bitwise Operators
C provides six bitwise operators for manipulating bit. The bitwise operator can only be applied to integral operands such as char, short, int and long. The following table illustrates C bitwise operators:
C Bitwise Operators | Description |
---|---|
& | Bitwise AND |
| | Bitwise inclusive OR |
^ | Bitwise exclusive OR |
<< | Bitwise left shift |
>> | Bitwise right shift |
~ | one’s complement |
The bitwise operators are preferred in some contexts because bitwise operations are faster than (+) and (-) operations and significantly faster than (*) and (/) operations.
Example of C bitwise Operators
Here is a simple program that demonstrates C bitwise operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* * File: main.c * Author: https://zentut.com/ * Purpose: Demonstrates C bitwise operators */ #include <stdio.h> void main() { int d1 = 4, /* binary 100 */ d2 = 6, /* binary 110 */ d3; printf("\nd1=%d", d1); printf("\nd2=%d", d2); d3 = d1 & d2; printf("\n Bitwise AND d1 & d2 = %d", d3); d3 = d1 | d2; printf("\n Bitwise OR d1 | d2 = %d", d3); d3 = d1 ^ d2; printf("\n Bitwise XOR d1 ^ d2 = %d", d3); d3 = ~d1; printf("\n Ones complement of d1 = %d", d3); d3 = d1 << 2; printf("\n Left shift by 2 bits d1 << 2 = %d", d3); d3 = d1 >> 2; printf("\n Right shift by 2 bits d1 >> 2 = %d", d3); } |
And here is the output:
1 2 3 4 5 6 7 8 | d1=4 d2=6 Bitwise AND d1 & d2 = 4 Bitwise OR d1 | d2 = 6 Bitwise XOR d1 ^ d2 = 2 Ones complement of d1 = -5 Left shift by 2 bits d1 << 2 = 16 Right shift by 2 bits d1 >> 2 = 1 |
C Increment Operator
C provides two operators for incrementing and decrementing the value of variables.
- The increment operator ++ adds 1 to its operand.
- The decrement operator — subtracts 1 from its operand
The C increment and decrement operators can be used either as prefix operator or postfix operators as follows:
1 2 3 4 | variable++; variable--; ++variable; --variable; |
The C increment operator in both prefix or postfix contexts is to add 1 to a variable. But the expression ++variable increments variable before its value is used, whereas variable++ increments variable after its value has been used.
Example of C Increment Operator
Let’s take a look at a simple example that demonstrates prefix and postfix of C increment operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /* * File: main.c * Author: zentut.com * Purpose: Demonstrates C increment operator */ #include <stdio.h> void main() { int x = 10; int y; /* prefix */ y = ++x; printf("x = %d\n",x); printf("y = %d\n",y); x = 10; /* postfix */ y = x++; printf("x = %d\n",x); printf("y = %d\n",y); return 0; } |
1 2 3 4 | x = 11 y = 11 x = 11 y = 10 |
Note that in both cases x is 11 however y is 11 in the first case and 19 in the second case.
So in the case that the variable value is not used, there is no difference between prefix or postfix of the increment operator.
C Logical Operators
You use C logical operators to connect expressions and/or variables to form compound conditions. The C logical expression returns an integer (int). The result has value 1 if the expression is evaluated to true otherwise it returns 0. C uses the following symbols for the Boolean operations AND, OR, and NOT.
Operator | Meaning |
---|---|
&& | logical AND |
|| | logical OR |
! | logical NOT |
- The || operator, their operands are evaluated in order from left to right, and if the value of the left operand is sufficient to determine the result of the operation, then the right operand is not evaluated at all.
- The operator && evaluates the right operand only if the left operand yields 1;
The following table illustrates the truth table of the logical operators:
a | b | a&&b | a||b | !a |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Example of C Logical Operators
The following program illustrates the C logical operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /* * File: main.c * Author: zentut.com * Purpose: A program demonstrates C logical operators */ #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int x = 10; int y = 15; int z = 20; if (x < y && y < z) { printf("x > y && y < z\n"); } if (x > y || y < z) { printf("x > y || y < z\n"); } if (!(x > y)) { printf("!(x > y)\n"); } return (0); } |
Below is the output of the C logical operators demo program:
1 2 3 | x > y && y < z x > y || y < z !(x > y) |
C Relational Operators
C provides you a list of relational operators to allows you to compare data. The relational operators enable you to check whether two variables or expressions are equal, not equal, which one is greater or less than other…etc. The relational operators are used in Boolean conditions or expressions to return 0 or 1.The following table illustrates the relational operators in C:
Relational Operators | Description |
---|---|
== | Equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Not equal to |
C relational operators example
The following program demonstrates the relational operators in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /* * File: main.c * Author: zentut.com * Purpose: A program demonstrates C relational operators */ #include <stdio.h> #include <stdlib.h> /** * Relational operators demo * @param x * @param y */ void demo(int x, int y) { printf("x = %d;y = %d\n", x, y); if (x == y) { printf("x is equal to y\n"); } else if (x != y) { printf("x is not equal to y\n"); if (x > y) { printf("x is greater than y\n"); } else { printf("x is less than y\n"); } } }; /* * main program */ int main(int argc, char** argv) { int x = 10; int y = 10; demo(x, y); x = 10; y = 20; demo(x, y); x = 20; y = 10; demo(x, y); return (0); } |
Here is the output of the demo program:
1 2 3 4 5 6 7 8 | x = 10;y = 10 x is equal to y x = 10;y = 20 x is not equal to y x is less than y x = 20;y = 10 x is not equal to y x is greater than y |
C Ternary Operator
C ternary operator is also referred as a conditional operator or trinary operator because:
- It is only the operator that has three operands.
- It is a shorthand of combination of the if-else and returns statement.
The syntax of C ternary operator is as follows:
1 | condition ? expression 1 : expression 2 |
First C evaluates the condition. Based on the return value of the condition, then it evaluates the second or third operand:
- If the condition is evaluated to true (1), only the second operand (expression 1) is evaluated. Then the operator returns the value of the expression 1.
- If the condition is evaluated to false (0), only the third operand (expression 2) is evaluated. The value of the expression 2 is returned.
The ternary operator can be rewritten as the if-else and return statement as follows:
1 2 3 4 | if(condition) return expression1; else return expression2; |
Example of C Ternary Operator
In this example, we are going to write an inline function that uses C ternary operator to find the minimum number between two integers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* * File: main.c * Author: zentut.com * Purpose: A program demonstrates C ternary operator */ #include <stdio.h> #include <stdlib.h> /** * Find min of two integers * @param x * @param y * @return minimum value of x and y */ inline int min(int x, int y) { return x <= y ? x : y; } int main(int argc, char** argv) { int x = 10; int y = 15; printf("x=%d,y=%d\n", x, y); printf("min(x,y)=%d\n", min(x, y)); return (0); } |
1 2 | x=10,y=15 min(x,y)=10 |