In the realm of C programming, the arithmetic operators are fundamental operators utilized for executing various mathematical operations like Addition, Subtraction, Multiplication, Division, Modulus, and more on variables and constants. These operators are integral in programming as they form the foundation for a wide range of computations, whether they are straightforward or intricate.
Types of Arithmetic Operators
There are primarily two categories of Arithmetic Operators in the C programming language. These consist of:
Here, we will discuss these operators one by one.
Binary Arithmetic Operators:
In the realm of C programming, binary arithmetic operators refer to operators necessitating two operands for executing fundamental mathematical operations. They serve the purpose of conducting Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modulus (%) on two specified values or variables.
1) Addition (+) Operator
The plus (+) operator is a basic symbol utilized for performing addition on two provided operands. It is applicable across various data types like integer, float, long, double, enumerated, and string types to sum up the specified operands.
For instance, suppose we have two operands, 8 and 15, and we aim to calculate the total of these values. In this case, we use the '+' operator to combine these operands, resulting in the integer value 23.
C Example using Addition (+) Operator
Let's consider an example to demonstrate the addition operator in the C programming language.
Example
#include <stdio.h>
int main ()
{
// using integer variables
int a, b, res;
// using float data type number
float x, y, res1;
// using double variables
double m, n, res2;
printf ("Enter two integer numbers: ");
scanf ("%d %d", &a, &b);
res = a + b; // use + operator
printf ("Enter two float numbers: \n");
scanf ("%f %f", &x, &y);
res1 = x + y; // use + operator
printf ("Enter two double data type numbers: \n");
scanf ("%lf %lf", &m, &n);
res2 = m + n; // use + operator
printf ("The sum of two integer numbers: %d \n", res);
printf ("The sum of two float numbers: %f \n", res1);
printf ("The sum of two double numbers: %lf", res2);
return 0;
}
Output:
Enter two integer numbers: 25
35
Enter two float numbers:
79.21
67.79
Enter two double data type numbers:
234.123
345.341
The sum of two integer numbers: 60
The sum of two float numbers: 147.000000
The sum of two double numbers: 579.464000
Explanation:
In this instance, we illustrate the binary arithmetic addition operator (+) in the C programming language utilizing various data types. We input two integers, two floats, and two doubles, perform addition with the + operator, and subsequently exhibit the resulting sums.
2) Subtraction (-) Operator
The minus symbol (-) represents the subtraction operator in programming. This operator calculates the result of subtracting the second number from the first number. In C programming, the numbers involved can have varying data types like int, float, double, long double, and more.
For instance, if we have two values, 10 and 2, and we desire to find the outcome of subtracting them, we employ the '-' symbol to calculate the result, which is 8.
C Example using Subtraction (-) Operator
Let's consider an example to demonstrate the subtraction operator in the C programming language.
Example
#include <stdio.h>
int main ()
{
// using integer variables
int a, b, res;
// using float data type number
float x, y, res1;
// using double variables
double m, n, res2;
printf ("Enter two integer numbers: ");
scanf ("%d %d", &a, &b);
res = a - b; // use - operator
printf ("Enter two float numbers: \n");
scanf ("%f %f", &x, &y);
res1 = x - y; // use - operator
printf ("Enter two double data type numbers: \n");
scanf ("%lf %lf", &m, &n);
res2 = m - n; // use - operator
printf ("The subtraction of two integer numbers: %d \n", res);
printf ("The subtraction of two float numbers: %f \n", res1);
printf ("The subtraction of two double numbers: %lf", res2);
return 0;
}
Output:
Enter two integer numbers: 56
11
Enter two float numbers:
78.98
56.45
Enter two double data type numbers:
47789.7149
1234.7987
The subtraction of two integer numbers: 45
The subtraction of two float numbers: 22.530003
The subtraction of two double numbers: 46554.916200
Explanation:
In this illustration, we showcase the subtraction operator (-) for binary arithmetic in the C programming language across various data types. Our inputs consist of pairs of integers, floats, and doubles. We perform the subtraction operation using the (-) operator on each pair and subsequently exhibit the resulting values.
3) Multiplication (*) Operator
The asterisk symbol (*) represents the multiplication operator, which is employed to calculate the product of two numbers, n1 and n2. These numbers can have varying data types like integer, floating point, or double.
For instance, when working with two values, such as 10 and 5, and aiming to calculate their multiplication, we can employ the '*' symbol to operate on these numbers and obtain an integer result of 50.
C Example using Multiplication (*) Operator
Let's consider an example to demonstrate the multiplication (*) operator in the C programming language.
Example
#include <stdio.h>
int main ()
{
// using integer variables
int a, b, res;
// using float data type number
float x, y, res1;
// using double variables
double m, n, res2;
printf ("Enter two integer numbers: ");
scanf ("%d %d", &a, &b);
res = a * b; // use * operator
printf ("Enter two float numbers: \n");
scanf ("%f %f", &x, &y);
res1 = x * y; // use * operator
printf ("Enter two double data type numbers: \n");
scanf ("%lf %lf", &m, &n);
res2 = m * n; // use * operator
printf ("The multiplication of two integer numbers: %d \n", res);
printf ("The multiplication of two float numbers: %f \n ", res1);
printf ("The multiplication of two double numbers: %lf", res2);
return 0;
}
Output:
Enter two integer numbers: 15
12
Enter two float numbers:
2.5
3.5
Enter two double data type numbers:
234.324
798.124
The multiplication of two integer numbers: 180
The multiplication of two float numbers: 8.750000
The multiplication of two double numbers: 187019.608176
Explanation:
In this instance, we showcase the binary arithmetic multiplication operator () in the C programming language utilizing various data types. We input two integers, two floating-point numbers, and two double-precision floating-point numbers, perform multiplication operations with the () operator, and subsequently exhibit their individual results.
4) Division (/) Operator
The division operator is a mathematical symbol that separates one value by another. By employing the division operator (/), we can separate variables of int, float, double, and long data types, and the outcome varies based on the data type of the values used. When integer data types are utilized, the operator provides the quotient without any remainder. Conversely, when floating-point and double data types are used, it yields accurate fractional outcomes.
For instance, if we have two values, 25.5 and 5.0, and we wish to calculate their quotient, we employ the '/' operator to divide the numbers, resulting in a floating-point value of 5.1.
C Example using Division (/) Operator
Let's consider an example to demonstrate the functionality of the division (/) operator in the C programming language.
Example
#include <stdio.h>
int main ()
{
// using integer variables
int a, b, res;
// using float data type number
float x, y, res1;
// using double variables
double m, n, res2;
printf ("Enter two integer numbers: ");
scanf ("%d %d", &a, &b);
res = a / b; // use / operator
printf ("Enter two float numbers: \n");
scanf ("%f %f", &x, &y);
res1 = x / y; // use / operator
printf ("Enter two double data type numbers: \n");
scanf ("%lf %lf", &m, &n);
res2 = m / n; // use / operator
printf ("The division of two integer numbers: %d \n", res);
printf ("The division of two float numbers: %f \n", res1);
printf ("The division of two double numbers: %lf", res2);
return 0;
}
Output:
Enter two integer numbers: 125
15
Enter two float numbers:
214.32
248.56
Enter two double data type numbers:
4243.7691
687.434
The division of two integer numbers: 8
The division of two float numbers: 0.862247
The division of two double numbers: 6.173348
Explanation:
In this instance, we showcase the functionality of the division (/) operator in the C programming language across various data types. We input two integers, floats, and doubles, divide the initial number by the second one in each scenario, and exhibit the outcomes. When integers are utilized, the output represents the quotient exclusively without any remainder, whereas with float and double data types, the results present precise decimal values.
5) Modulus (%) Operator
The modulus operator, denoted by the percentage sign (%), calculates the remainder resulting from dividing the first number by the second number. In C programming, this operator exclusively works with integers and does not support floats or doubles.
For instance, if we have two values, 25 and 4, and we aim to calculate the modulus outcome, we employ the '%' symbol to find the remaining value of 1 after division.
C Example using Modulus (%) Operator
Let's consider a scenario to demonstrate the modulus (%) operator in the C programming language.
Example
#include <stdio.h>
int main ()
{
// declare integer variables
int a, b, res;
printf ("Enter two integer numbers: ");
scanf ("%d %d", &a, &b);
res = a % b; // use % operator
printf ("The modulus of two integer numbers: %d \n", res);
return 0;
}
Output:
Enter two integer numbers: 35
3
The modulus of two integer numbers: 2
Explanation:
In this instance, we are provided with two integer values for computation. Subsequently, we determine the modulus of these values by dividing the initial number by the second one, and then exhibit the outcome.
Unary Arithmetic Operator
In the realm of C programming, unary operators are operators that execute actions on a single operand. There exist primarily two categories of unary arithmetic operators. These categories are as outlined below:
1) Increment (++) Operator
In the realm of C programming, the increment Operator serves as a form of Arithmetic operator, symbolized by the double plus (++) operator. This operator is employed to augment the integer value by 1.
Syntax:
It has the following syntax:
B = A++;
B = ++A;
For instance, if A holds the integer value of 10, the increment operator boosts the operand value by 1, resulting in a return value of 11.
C Example using increment (++) operator
Let's consider a scenario to demonstrate the usage of the increment (++) operator in the C programming language.
Example
#include <stdio.h>
int main () {
int a = 5;
printf ("Pre-increment: %d\n", ++a); // Increments before printing
a = 5;
printf ("Post-increment: %d\n", a++); // Prints first, then increments
printf ("After Post-increment: %d\n", a);
return 0;
}
Output:
Pre-increment: 6
Post-increment: 5
After Post-increment: 6
Explanation:
In this illustration, we showcase the application of pre-increment and post-increment operators. With the pre-increment (++a), the variable a is incremented before displaying its value. Conversely, using the post-increment (a++), the current value is printed first, followed by the increment operation.
2) Decrement Operator
The double minus (--) symbol represents the decrement operator, responsible for reducing the operand value by 1.
Syntax:
It has the following syntax:
B = A--;
B= --A;
For instance, let's consider that the integer value of A is 10. When the decrement operator is applied, it reduces the operand value by 1, resulting in a return value of 9.
C Example using Decrement (--) Operator
Let's consider an example to demonstrate the decrement (--) operator in the C programming language.
Example
#include <stdio.h>
int main () {
int a = 10;
printf ("Pre-decrement: %d\n", --a); // Decreases first
a = 10;
printf ("Post-decrement: %d\n", a--); // Prints first, then decreases
printf ("After Post-decrement: %d\n", a);
return 0;
}
Output:
Pre-decrement: 9
Post-decrement: 10
After Post-decrement: 9
Explanation:
In this illustration, we showcase the pre-decrement and post-decrement operators. When executing the pre-decrement operation, the value of b is reduced initially, followed by displaying the result. Conversely, when executing the post-decrement operation, the current value is displayed first, and then the decrement is applied.
C Example using Multiple Arithmetic Operators
Let's consider an example to showcase the various arithmetic operators in C programming.
Example
#include <stdio.h>
int main () //main function
{
int length = 15, width = 10, height = 8;
int volume, perimeter, surfaceArea;
int avg_dimension;
int complex_calc;
// Arithmetic operations used in different cases
volume = length * width * height; // using Multiplication
perimeter = 2 * (length + width); // using Addition and Multiplication
surfaceArea = 2 * (length * width + width * height + height * length);
avg_dimension = (length + width + height) / 3; // using addition and division
// Complex expression using multiple arithmetic operators
complex_calc = (volume / avg_dimension) + (surfaceArea % length) - (perimeter * 2);
printf ("Dimensions: length = %d, width = %d, height = %d\n", length, width, height);
printf ("Volume = %d\n", volume);
printf ("Perimeter = %d\n", perimeter);
printf ("Surface Area = %d\n", surfaceArea);
printf ("Average Dimension = %d\n", avg_dimension);
printf ("Complex Calculation = %d\n", complex_calc);
return 0;
}
Output:
Dimensions: length = 15, width = 10, height = 8
Volume = 1200
Perimeter = 50
Surface Area = 700
Average Dimension = 11
Complex Calculation = -13
Explanation:
In this instance, we showcase the application of several arithmetic operators within a single program. Here, we determine the capacity, boundary length, and external area of a rectangular parallelepiped utilizing different functions. Following that, we compute the mean of measurements using the division operator and execute an intricate computation that integrates division, modulus, deduction, and multiplication operators.
Conclusion
In summary, arithmetic operators play a crucial role in C programming, allowing us to execute various mathematical calculations such as addition, subtraction, multiplication, and division. These operators are widely employed across different domains like mathematical computations, analytical tasks, and practical scenarios. Mastering these operators empowers us to develop efficient logical and numerical algorithms.
Arithmetic Operators FAQs
Operator precedence plays a crucial role in determining the order in which arithmetic operations are carried out in C programming. It establishes the sequence in which various operators are evaluated within an expression, ensuring that the correct mathematical outcome is achieved. By following the rules of operator precedence, C executes operations such as addition, subtraction, multiplication, and division in a specific order to prevent ambiguity and ensure accurate computation results.
In C programming, the concept of operator precedence dictates the sequence in which arithmetic operations are carried out within intricate expressions. For instance, multiplication and division are prioritized over addition and subtraction. Failing to use parentheses can lead to unexpected outcomes due to precedence rules.
In C, the distinction between integer division and floating-point division lies in how the operands are treated and the result they produce:
- Integer division involves dividing two integers, which results in a quotient that is also an integer. Any fractional part is truncated, and only the whole number portion is retained.
- Floating-point division, on the other hand, deals with operands that are floating-point numbers. The result of this division is a floating-point number, preserving both the integer and fractional parts of the quotient.
By employing integer division, any fractional part is discarded, leaving only the quotient. On the other hand, floating-point division does not round off decimals, leading to more precise outcomes. The nature of the division performed is dictated by the data types of the operands.
The behavior of the modulus operator (%) with negative numbers in C is that the result takes the sign of the dividend, not the divisor. This means that when a negative number is divided by a positive number, the result will be negative.
The sign of the dividend (numerator) determines the sign of the output when using the modulus operator in C. For example, -10 % 3 results in -1, while 10 % -3 equals 1. This behavior may exhibit slight variations across different programming languages, but remains consistent in C.
4) Can arithmetic operators be combined with assignment operators in the C programming language?
Operators like +=, -=, *=, /=, and %= combine arithmetic operations with assignments in a single step. For instance, x += 5 has the same effect as x = x + 5. These condensed expressions not only improve the clarity of code but also boost its efficiency.
5) What is the use of %d in C programming?
In C programming, the %d is utilized to denote a signed decimal integer.