Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operator | Meaning of Operator |
---|---|
+ | addition or unary plus |
- | subtraction or unary minus |
* | multiplication |
/ | division |
% | remainder after division (modulo division) |
Arithmetic Operations On Numbers
Warning
- The
%
operator can only be used with integers.- When using the
/
operator the result will be an integer if bought the values are integers
Strange Case
In normal calculation, 9/4 = 2.25
. However, the output is 2
in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2
instead of 2.25
.
Read more: Strange Case:
Example:
Arithmetic Operators On Arrays Of Numbers
You cannot use arithmetic operators on arrays of numbers directly in C.
In C, an array is a collection of elements of the same data type. To perform arithmetic operations on the elements of an array, you need to access each element individually and perform the operation.
Here’s an example of how you can add two arrays of integers in C:
So while you cannot perform arithmetic operations on arrays directly in C, you can iterate over the elements of an array and perform operations on each element individually.
Arithmetic Operators On C Chars
The arithmetic operators, when used with char
variables, they perform arithmetic operations on their ASCII values, which are represented as integers.
For example:
Arithmetic Operators On C Strings
- Arithmetic operators are not designed to work with strings, which are C Arrays of characters.