Arithmetic Operators in C are the symbols utilized to execute fundamental mathematical tasks. These operators are straightforward and accommodate a variety of data types. In this article, we will explore the concept of arithmetic operators in C, their classifications, precedence and associativity, examples of their use in C programming, benefits, and guidelines for effectively employing arithmetic operators in C.
Arithmetic operators in C are the symbols that facilitate arithmetic computations on numerical values. These operators execute operations such as addition(+), subtraction(-), multiplication(*), division(/), and modulus(%).
Syntax of Arithmetic Operators in C
The syntax of arithmetic operators in C is quite simple, as the operators are positioned between two operands to carry out calculations.
General Syntax:
result = operand1 operator operand2;
In this context:
operand1 and operand2 represent the numerical values or variables.
operator is among the arithmetic operators (+, -, *, /, %).
result holds the calculated value.
Types of Arithmetic Operators in C
There exist five categories of arithmetic operators, differentiated by the operations they perform.
Here is the enumeration of arithmetic operators in C:
Operator Name
Symbol
Description
Syntax
Addition
+
Combines two numbers
x + y
Subtraction
–
Removes one number from another
x – y
Multiplication
*
Multiplies two numbers together
x * y
Division
/
Divides one number by another (yields the quotient)
x / y
Modulus
%
Provides the remainder of integer division
x % y
Let’s delve into each type with brief explanations and examples in C.
1. Addition Operator (+)
The addition operator in C is employed to sum two numbers or operands.
Syntax:
result = operand1 + operand2;
Example:
C
Code Copied!
var isMobile = window.innerWidth nint main() {n int x = 10, y = 5;n int sum = x + y; // Addition operationn printf(“Sum of %d and %d is: %dn”, x, y, sum);n return 0;n}n”);
function decodeHTML77390(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard77390() {
const code = editor77390.getValue();
navigator.clipboard.writeText(code).then(() => {
jQuery(“.maineditor77390 .copymessage”).show();
setTimeout(function() {
jQuery(“.maineditor77390 .copymessage”).hide();
}, 2000);
}).catch(err => {
console.error(“Error copying code: “, err);
});
}
function runCode77390() {
var code = editor77390.getSession().getValue();
jQuery(“#runBtn77390 i.run-code”).show();
jQuery(“.output-tab”).click();
jQuery.ajax({
url:
“““html
“https://intellipaat.com/blog/wp-admin/admin-ajax.php”,
type: “post”,
data: {
language: “c”,
code: code,
cmd_line_args: “”,
variablenames: “”,
action:”compilerajax”
},
success: function(response) {
var myArray = response.split(“~”);
var data = myArray[1];
jQuery(“.output77390”).html(“
"+data+"");
jQuery(".maineditor77390 .code-editor-output").show();
jQuery("#runBtn77390 i.run-code").hide();
}
})
}
function closeoutput77390() {
var code = editor77390.getSession().getValue();
jQuery(".maineditor77390 .code-editor-output").hide();
}
// Link event listeners to the buttons
document.getElementById("copyBtn77390").addEventListener("click", copyCodeToClipboard77390);
document.getElementById("runBtn77390").addEventListener("click", runCode77390);
document.getElementById("closeoutputBtn77390").addEventListener("click", closeoutput77390);
Result:
In this snippet, the addition operator computes the sum of two integers, x and y, valued at 10 and 5, producing an output of 15 in the console.
2. Subtraction Operator (-)
The subtraction operator in C is employed to deduct one operand from another.
Structure:
result = operand1 - operand2;
Illustration:
C
Code Copied!
var isMobile = window.innerWidth ");
editor368.setValue(decodedContent); // Assign the default text
editor368.clearSelection();
editor368.setOptions({
maxLines: Infinity
});
function decodeHTML368(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard368() {
const code = editor368.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
data: {
language: "c",
code: code,
cmd_line_args: "",
variablenames: "",
action:"compilerajax"
},
success: function(response) {
var myArray = response.split("~");
var data = myArray[1];
jQuery(".output368").html("
"+data+"");
jQuery(".maineditor368 .code-editor-output").show();
jQuery("#runBtn368 i.run-code").hide();
}
})
}
function closeoutput368() {
var code = editor368.getSession().getValue();
jQuery(".maineditor368 .code-editor-output").hide();
}
// Link event listeners to the buttons
document.getElementById("copyBtn368").addEventListener("click", copyCodeToClipboard368);
document.getElementById("runBtn368").addEventListener("click", runCode368);
document.getElementById("closeoutputBtn368").addEventListener("click", closeoutput368);
Result:
In this section of code, the subtraction operator computes the difference between two integers, a = 10 and b = 5, yielding an output of 5 in the console.
3. Multiplication Operator (*)
The multiplication operator in C is utilized to calculate the product of two values or operands.
Structure:
result = operand1 * operand2;
Illustration:
C
Code Copied!
var isMobile = window.innerWidth "");
editor26879.setValue(decodedContent); // Assign the default text
editor26879.clearSelection();
editor26879.setOptions({
maxLines: Infinity
});
function decodeHTML26879(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard26879() {
const code = editor26879.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
jQuery.ajax({
url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php",
type: "post",
data: {
language: "c",
code: code,
cmd_line_args: "",
variablenames: "",
action:"compilerajax"
},
success: function(response) {
var myArray = response.split("~");
var data = myArray[1];
jQuery(".output26879").html("
"+data+"");
jQuery(".maineditor26879 .code-editor-output").show();
jQuery("#runBtn26879 i.run-code").hide();
}
});
}
function closeoutput26879() {
var code = editor26879.getSession().getValue();
jQuery(".maineditor26879 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn26879").addEventListener("click", copyCodeToClipboard26879);
document.getElementById("runBtn26879").addEventListener("click", runCode26879);
document.getElementById("closeoutputBtn26879").addEventListener("click", closeoutput26879);
Output:
This code demonstrates how the multiplication operator calculates the product of two integers, a and b, which are assigned the values of 10 and 5, and outputs the result to the console.
4. Division Operator (/)
The division operator in C serves to divide one number by another, yielding a quotient as the result.
Syntax:
result = operand1 / operand2;
Example:
C
Code Copied!
var isMobile = window.innerWidth "");
editor35035.setValue(decodedContent); // Assign the default text
editor35035.clearSelection();
editor35035.setOptions({
maxLines: Infinity
});
function decodeHTML35035(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function for copying code to clipboard
function copyCodeToClipboard35035() {
const code = editor35035.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
jQuery.ajax({
url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php",
type: "post",
data: {
language: "c",
code: code,
cmd_line_args: "",
variablenames: "",
action:"compilerajax"
},
success: function(response) {
var myArray = response.split("~");
var data = myArray[1];
jQuery(".output35035").html("
"+data+"");
jQuery(".maineditor35035 .code-editor-output").show();
jQuery("#runBtn35035 i.run-code").hide();
}
});
}
function closeoutput35035() {
var code = editor35035.getSession().getValue();
jQuery(".maineditor35035 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn35035").addEventListener("click", copyCodeToClipboard35035);
document.getElementById("runBtn35035").addEventListener("click", runCode35035);
document.getElementById("closeoutputBtn35035").addEventListener("click", closeoutput35035);
Output:
``````html
In this script, the division operator segments the integer a by b and the float x by y, subsequently the outcomes of both divisions are displayed in the console.
5. Modulus Operator (%)
The modulus operator in C is utilized to yield the remainder from the division of two integers.
jQuery.ajax({
url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php",
type: "post",
data: {
language: "c",
code: code,
cmd_line_args: "",
variablenames: "",
action:"compilerajax"
},
success: function(response) {
var myArray = response.split("~");
var data = myArray[1];
jQuery(".output40135").html("
"+data+"");
jQuery(".maineditor40135 .code-editor-output").show();
jQuery("#runBtn40135 i.run-code").hide();
}
});
}
function closeoutput40135() {
jQuery(".maineditor40135 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn40135").addEventListener("click", copyCodeToClipboard40135);
document.getElementById("runBtn40135").addEventListener("click", runCode40135);
document.getElementById("closeoutputBtn40135").addEventListener("click", closeoutput40135);
Output:
In this script, the modulus operator yields a remainder of 1 after dividing two integers, a = 10 and b = 3, which represents the remainder of that division.
Precedence and Associativity of Arithmetic Operators in C
The operator precedence verifies the sequence in which operations occur. The operators with greater precedence are computed first.
The associativity determines the order of evaluation. The arithmetic operators possess left-to-right associativity, implying that operations are processed from left to right for operators of equal precedence.
Here is a compilation of the precedence levels and associativity of the arithmetic operators:
Operator
Precedence
Associativity
*
Highest
Left to Right
/
Higher
Left to Right
%
Higher
Left to Right
+
Lower
Left to Right
–
Lowest
Left to Right
Examples of Arithmetic Operators in C Programming
Here are several instances of arithmetic operators in C programming:
1. Computing the Area of a Circle
This C program employs the multiplication operator to compute the area of a circle using the formula PI * r * r, with the radius provided by the user.
C
Code Copied!
Output:
2. Compound Interest Calculation
This C program computes the compound interest using the equation A = P * (1 + r/100)^t. In this equation, P represents the principal, r symbolizes the rate, and t denotes the time. The pow() function facilitates the exponentiation.
C
Code Copied!
Output:
``````html
3. Mean of Two Numbers
This C program illustrates how the mean of two numbers is computed using the equation [(x + y) / 2], and afterwards, the outcome is displayed with two decimal points.
function closeoutput2415() {
var code = editor2415.getSession().getValue();
jQuery(".maineditor2415 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn2415").addEventListener("click", copyCodeToClipboard2415);
document.getElementById("runBtn2415").addEventListener("click", runCode2415);
document.getElementById("closeoutputBtn2415").addEventListener("click", closeoutput2415);
Outcome:
4. Converting Temperature from Celsius to Fahrenheit
This C program transforms Celsius into Fahrenheit utilizing the equation F = (C * 9/5) + 32, and subsequently, the outcome is displayed with two decimal points.
C
Code Copied!
var isMobile = window.innerWidth "");
editor73035.setValue(decodedContent); // Set the default text
editor73035.clearSelection();
editor73035.setOptions({
maxLines: Infinity
});
function decodeHTML73035(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard73035() {
const code = editor73035.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor73035 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor73035 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode73035() {
var code = editor73035.getSession().getValue();
function closeoutput73035() {
var code = editor73035.getSession().getValue();
jQuery(".maineditor73035 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn73035").addEventListener("click", copyCodeToClipboard73035);
document.getElementById("runBtn73035").addEventListener("click", runCode73035);
document.getElementById("closeoutputBtn73035").addEventListener("click", closeoutput73035);
Output:
5. Determine Even or Odd Number
This C program verifies if the number provided by the user is even or odd by utilizing the modulus operator.
C
Code Copied!
var isMobile = window.innerWidth ");
editor73997.setValue(decodedContent); // Set the default text
editor73997.clearSelection();
editor73997.setOptions({
maxLines: Infinity
});
function decodeHTML73997(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard73997() {
const code = editor73997.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput73997() {
var code = editor73997.getSession().getValue();
jQuery(".maineditor73997 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn73997").addEventListener("click", copyCodeToClipboard73997);
document.getElementById("runBtn73997").addEventListener("click", runCode73997);
document.getElementById("closeoutputBtn73997").addEventListener("click", closeoutput73997);
Output:
Benefits of Arithmetic Operators in C
Arithmetic operators are quick and efficient.
They have uncomplicated syntax that allows for simpler calculations.
They function with various data types like int, float, and double for computations.
Arithmetic operators enhance code readability.
They are extensively utilized in fields such as scientific and financial computations.
Optimal Practices for Using Arithmetic Operators in C
It is essential to use ( ) brackets to ensure the correct order of operations.
You should always verify if the denominator is zero prior to executing division to prevent runtime errors.
Exercise caution during integer division operations.
Select appropriate data types, such as int, float, and double, according to the requirements of calculations.
The modulus operator should exclusively be used with integers, as it is not applicable to other data types.
Store results in variables rather than recalculating the same value multiple times.
Maintain proper and consistent spacing around the operators to enhance readability.
Conclusion
In summary, as discussed above, Arithmetic Operators in C are utilized for computations. They not only accommodate different data types, but these operators are also frequently employed in scientific, financial, or general programming applications. Thus, with an understanding of precedence, associativity, and optimal practices, one can carry out calculations effectively.
``````html
and swiftly in C programs.
Arithmetic Operators in C – FAQs
Q1. What is the function of arithmetic operators in C?
The arithmetic operators execute fundamental mathematical tasks.
Q2. Is it possible to use modulus with floating-point numbers?
No, the modulus operator cannot be applied to them.
Q3. What happens if I divide by zero?
This results in a runtime error or unpredictable behavior.
Q4. How can I carry out exponentiation in C?
You may utilize pow() from <math.h> to execute exponentiation in C.
Q5. Are arithmetic operators applicable in if conditions?
Indeed, arithmetic operators can be employed in if conditions for making decisions.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.