Operators in C with Code Examples

In this tutorial, we will discuss the different C operator with a code explanation. In a C program, the operators are a symbol that performs operations on a value or in a variable. For e.g., (-), (+), (*), (/), these are the operators to perform subtraction, addition, multiplication and division.

Furthermore, C language has a built-in operators which are the following:

• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Operators Precedence

Arithmetic Operators in C Language with Examples

The arithmetic operator performs specific mathematical operations like multiplication, division, subtraction, and addition in numerical values such as constant and variables.

Let’s have a look at the example of arithmetic operators below:

``````// arithmetic operators examples
#include <stdio.h>
int main()
{
int x = 8,y = 5, z;

z = x+y;
printf("x+y = %d \n",z);
z = x-y;
printf("x-y = %d \n",z);
z = x*y;
printf("x*y = %d \n",z);
z = x/y;
printf("x/y = %d \n",z);
z = x%y;
printf("Remainder when x divided by y = %d \n",z);

return 0;
}``````

Output:

x+y = 13
x-y = 3
x*y = 40
x/y = 1
Remainder when x divided by y = 3

Here are the explanations of the example above.

• The (+) symbol operator refers to a computation of addition.
• The () symbol operator refers to a computation of subtraction.
• The (*) symbol operator refers to a computation of multiplication.
• The (/) symbol operator refers to a computation of a division.
• For a normal calculation 8/5 = 1.6. On the other hand, the output to run the program is 1.
• Because both of the variables x and y are integers. However, the output result will be also an integer.
• In C program, a compiler declines the term after a decimal point and it will show the answer 1 instead of 1.6.
• The (%) module symbol operator refers to the computation of a remainder. If x=8 is divided by y=5, the remainder will be 3.
• Note that % operator will be used with integers only.

Lets have another example: We assume x = 12.0, y = 5.0, z = 10, and w = 2

``````// neither one of the operands is a floating-point number
x/y = 2.4
x/w = 6
y/x = 0.41

// Both operands are integers
z/w = 5``````

Increment and Decrement Operators in C with Examples

In a C program, has usually two operators which are the increment (++) operator and the other one is decrement (–). When changing the value of an operand(variable or constant) by 1.

Increment(`++`) increases the value by 1 whereas decrement(`--`) decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

In addition, here are few examples below:

• ++i and i++: it means the result is i=i+1
• –k and k−−: it means the result is k=k-1.
``````// Here are the example of increment and decrement operators
#include <stdio.h>
int main()
{
int i = 13, k = 85;
float l = 9.9, m = 95.9;

printf("++i = %d \n", ++i);
printf("--k = %d \n", --k);
printf("++l = %f \n", ++l);
printf("--m = %f \n", --m);

return 0;
}``````

Output:

++i = 14
–k = 84
++l = 10.900000
–m = 94.900002

In a code example above, the operators (`++`) and (`--`) are have been used as prefixes. These two operators `a++` and `a--` are used for postfixes.

Assignment Operator in C with Examples

In a C language, the assignment operator is usually used for assigning a value into a variable in a code program. Besides, an assignment operators are also applied for assigning the result of a statement to a variables. The operator mostly plays an important role in assigning values to any variable. The most frequent operator is =.

In addition, in a C program have a collection has a compilation of a few assignment operators, which can be used for a C programming language. In a table below, are the lists of all the assignment operators, which is supported by the C language:

For example:

``````// An Example of assignment operators
#include <stdio.h>
int main()
{
int x = 17, y;

y = x;
printf("y = %d\n", y);
y += x;
printf("y = %d\n", y);
y -= x;
printf("y = %d\n", y);
y *= x;
printf("y = %d\n", y);
y /= x;
printf("y = %d\n", y);
y %= x;
printf("y = %d\n", y);

return 0;
}``````

Output:

y = 17
y = 34
y = 17
y = 289
y = 17
y = 0

Relational Operator in C with Examples

In a C program, the relational operator is commonly used to match two values in a program. It analyzes the relationship between the two operands. When the given relationship is true, probably it will return 1 and when the relationship is false, then it returns 0.

Relational operators are mostly used in decision making and loops. Let’s have a look at the table below.

Let’s have a look at the example of a relational operator below

``````// Working of relational operators
#include <stdio.h>
int main()
{
int x = 8, y = 8, z = 30;

printf("%d == %d is %d \n", x, y, x == y);
printf("%d == %d is %d \n", x, z, x == z);
printf("%d > %d is %d \n", x, y, x > y);
printf("%d > %d is %d \n", x, z, x > z);
printf("%d < %d is %d \n", x, y, x < y);
printf("%d < %d is %d \n", x, z, x < z);
printf("%d != %d is %d \n", x, y, x != y);
printf("%d != %d is %d \n", x, z, x != z);
printf("%d >= %d is %d \n", x, y, x >= y);
printf("%d >= %d is %d \n", x, z, x >= z);
printf("%d <= %d is %d \n", x, y, x <= y);
printf("%d <= %d is %d \n", x, z, x <= z);

return 0;
}``````

Output:

8 == 8 is 1
8 == 30 is 0
8 > 8 is 0
8 > 30 is 0
8 < 8 is 0
8 < 30 is 1
8 != 8 is 0
8 != 30 is 1
8 >= 8 is 1
8 >= 30 is 0
8 <= 8 is 1
8 <= 30 is 1

Logical Operator in C with Example

An expression containing a logical operator in C programming returns either 0 or 1. It depends upon the condition whether the expression results are either true or false. Besides, the logical operators are usually used for decision-making in a C language.

Furthermore, there are three logical operators that we need to evaluate more than one condition to make decisions. These logical operators are the following:

• && (it means logical AND)
• || (it means logical OR)
• ! (it means logical NOT)

Let’s have a look at the example of logical operator below.

``````// This is the examples of logical operators

#include <stdio.h>
int main()
{
int x = 12, y = 12, z = 40, output;

output = (x == y) && (z > y);
printf("(x == y) && (z > y) is %d \n", output);

output = (x == y) && (z < y);
printf("(x == y) && (z < y) is %d \n", output);

output = (x == y) || (z < y);
printf("(x == y) || (z < y) is %d \n", output);

output = (x != y) || (z < y);
printf("(x != y) || (z < y) is %d \n", output);

output = !(x != y);
printf("!(x != y) is %d \n", output);

output = !(x == y);
printf("!(x == y) is %d \n", output);

return 0;
}

``````

Output:

(x == y) && (z > y) is 1
(x == y) && (z < y) is 0
(x == y) || (z < y) is 1
(x != y) || (z < y) is 0
!(x != y) is 1
!(x == y) is 0

The explanation of logical operator program is above.

• `(x == y) && (z > 12)` It evaluates to 1 because both operands `(x == y)` and `(z > y)` is 1, then the result is true.
• `(x == y) && (z < y)` It evaluates to 0 because operand `(z < y)` is 0, then the result will be false.
• `(x == y) || (z < y)` It evaluates to 1 because `(x = y)` is 1, then the result is true.
• `(x != y) || (z < y)` It evaluates to 0 because both operand `(x != y)` and `(z < y)` are 0, then it will result to false.
• `!(x != y)` It evaluates to 1 because operand `(x != y)` is 0, then the result is false. However, !(x != y) is 1, then it will result true.
• `!(x == y)` It evaluates to 0 because `(x == y)` is 1, then it will result true. However, `!(x == y)` is 0, then it will result false.

Bitwise Operators in C with Examples

In a C language, the bitwise operators are the operators which work on bits and operate the bit-by-bit operation. It performs mathematical operations such as addition, subtraction, multiplication, division, and etc.

It is changed to a bit-level, which speeds up the processing and makes it simpler to implement during computation and software compilation.

In addition, bitwise operators are commonly used in C programming for implementing bit-level operations. In the table below are the symbol and name of the operators.

The others common operators

Comma Operator

The comma operators are used to link relevant statements together.

Let’s have a look at an example of comma operator:

``int x, y = 10, z;``

The sizeof operator

In C language, a `sizeof` is a unary operator that returned the size of data like constants, variables, array, structure, and etc.

Let’s have a look at an example size of operator:

``````#include <stdio.h>
int main()
{
int x;
float y;
double z;
char d;
printf("The Size of int=%lu bytes\n",sizeof(x));
printf("The Size of float=%lu bytes\n",sizeof(y));
printf("The Size of double=%lu bytes\n",sizeof(z));
printf("The Size of char=%lu byte\n",sizeof(d));

return 0;
}``````

Output:

The Size of int=4 bytes
The Size of float=4 bytes
The Size of double=8 bytes
The Size of char=1 byte

Conclusion

In this tutorial on Operators in C, we have discussed and learned almost all the Operators in C with proper explanations of examples. The tutorials begin with a brief introduction to Operators in C followed by details of the different types of Operators in C.

Furthermore, we have given a brief overview of all the Operators in C language and explained the basic introduction of the operators like arithmetic, increment/decrement, assignment, relational, logical, bitwise, comma operator, and also the sizeof operator.

After the overview, we also discussed the topic with an example for an easy understanding of the topic. The other important operators like comma and sizeof which are very useful in C programming language have been discussed as well.

We hope through this tutorial you could improve your knowledge of Operators in C and learn how we can use it for our software development projects.