Arithmetic instructions in C
A variable name appears on the left side of the = symbol, and variable names and constants appear on the right side of the = symbol in a C arithmetic instruction. The arithmetic operators +, -, *, and / link the variables and constants that appear on the right side of =. They are employed in the execution of mathematical operations on constants or variables. Addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) are the fundamental arithmetic operations.
For example;
int ex;
float ap, boo, sep, tax, req;
ex = 430;
ap = 23.127;
boo = sep * tax / req + 4.6 – 8/7;
Here,
The arithmetic operators are *, /, -, and +.
The assignment operator is =.
The constants 8, 7, and 430 are all integers.
The constants 4.6 and 23.127 are real.
ex is a variable with an integer value.
Real variables are ap, boo, sep, tax, and req.
"Operands" refers to the variables and constants statement taken together. The "arithmetic operators" operate on the operands on the right side of the equation when it is being executed, and the assignment operator is used to assign the result to the variable on the left side.
Arithmetic statement types include:
There are three possible types of C arithmetic statements. These are listed in the following order:
- Arithmetic statement in integer mode: All operands in this statement are either integer constants or integer variables.
int ij, xy, rs, qz;
ij = ij + 5;
xy = ij * rs + qz / 4 – 72;
2. The real mode arithmetic statement: All operands in this statement are real variables or real constants.
float rex, tk, sid, kip, are, yui;
sid = kip + 87.45 / 341.2 - 34.123;
are = sid + yui * tk / 56.3;
3. Statement in mixed mode arithmetic: In this statement, some operands are real and some are integers.
float std, frm, smp, mul ;
int p, q, r, sum;
mul = std * frm * smp * 4.32 ;
sum = p + q + r + 7;
Arithmetic Operator Types in C
Depending on how many operands they can handle, there are two categories of C arithmetic operators. These are listed in the following order:
- Arithmetic Operators in Binary
- Arithmetic Operators in Unary
Arithmetic Operators in Binary
Two operands are used by the C binary arithmetic operators to operate or work on. For executing arithmetic operations, C offers the following 5 Binary Arithmetic Operators:
Operator | The Operator's name | Operation Arithmetic | Syntax |
/ | Division | The first operand and the second operand are divided. | p / q |
* | Multiplication | Two operands are multiplied. | p * q |
+ | Addition | Two operands are added. | p + q |
- | Subtraction | Take the first operand and subtract it from the second operand. | p - q |
% | Modulus | When you divide the first operand by the second operand, find the remainder. | p % q |
Program:
#include <stdio.h>
int main()
{
int p = 15, q = 12, result;
printf("The value of p is %d and the value of q is %d\n", p, q);
result = p + q;
printf("The addition of p and q is %d\n", result);
result = p - q;
printf("The difference between p and q is %d\n", result);
result = p * q;
printf("The product of p and q is %d\n", result);
result = p / q;
printf("The division of p and q gives %d\n", result);
result = p % q;
printf("The remainder when p divides q is %d\n", result);
return 0;
}
Output:
Arithmetic Operators in Unary
Only one operand is used in the operations of the unary arithmetic operators. Two unary arithmetic operators exist in C, and they are as follows:
Operator | The Operator's name | Operation Arithmetic | Syntax |
-- | Decrement Operator | Reduces the variable's integer value by one. | |
++ | Increment Operator | Increases the variable's integer value by one. | |
- | Unary minus operator | yields the operand's negative value back. | |
+ | Unary Plus Operator | yields the operand's value back. |
Program:
#include <stdio.h>
int main()
{
int p = 15, result;
printf("Post Increment and Decrement\n");
result = p++;
printf("p is %d and result is %d\n", p,
result);
result = p--;
printf("p is %d and result is %d\n", p,
result);
printf("\nPre Increment and Decrement\n");
result = ++p;
printf("p is %d and result is %d\n", p, result);
result = --p;
printf("p is %d and result is %d\n", p, result);
return 0;
}
Output: