Arithmetic Operators in C

Last Updated :

Arithmetic operators in C are used for performing mathematical operations, such as addition, subtraction, multiplication, and division. Here is a list of arithmetic operators in C:

1. Addition (+): Adds two operands together.

int a = 10;
int b = 20;
int sum = a + b; // sum = 30

2. Subtraction (-): Subtracts one operand from another.

int a = 20;
int b = 10;
int difference = a - b; // difference = 10

3. Multiplication (*): Multiplies two operands.

int a = 10;
int b = 20;
int product = a * b; // product = 200

4. Division (/): Divides one operand by another.

int a = 20;
int b = 10;
int quotient = a / b; // quotient = 2

Note that if the operands of the division operator are integers, the result will be an integer, which means the result will be truncated if the division doesn’t result in a whole number. For example, if we divide 5 by 2, the result will be 2, not 2.5.

5. Modulo (%): Returns the remainder of the division of one operand by another.

int a = 20;
int b = 10;
int remainder = a % b; // remainder = 0

In this example, since 20 is evenly divisible by 10, the remainder is 0.

Arithmetic operators can be combined with assignment operators to form compound assignments, which can be more efficient and concise. For example, the following code adds 5 to a variable a and assigns the result back to a:

int a = 10;
a += 5; // equivalent to a = a + 5;

Similarly, we can use other arithmetic operators in combination with assignment operators, such as -=, *=, /=, and %=, to perform the corresponding operation and assign the result back to the variable.

Example:
Here is an example of using arithmetic operators in C:

#include <stdio.h>

int main() {
   int a = 10;
   int b = 20;
   int sum = a + b;
   int difference = a - b;
   int product = a * b;
   int quotient = b / a;
   int remainder = b % a;

   printf("a = %d, b = %d\n", a, b);
   printf("a + b = %d\n", sum);
   printf("a - b = %d\n", difference);
   printf("a * b = %d\n", product);
   printf("b / a = %d\n", quotient);
   printf("b %% a = %d\n", remainder);

   // Compound assignments
   a += 5;
   b -= 10;
   printf("a = %d, b = %d\n", a, b);

   return 0;
}

In this example, we first declare two integer variables a and b, and then we perform various arithmetic operations using the arithmetic operators +, -, *, /, and %, and assign the results to new variables sum, difference, product, quotient, and remainder. We then print the values of these variables using printf.

We also demonstrate the use of compound assignments, which combine an arithmetic operator with an assignment operator to perform the corresponding operation and assign the result back to the variable. We add 5 to a using the += operator, and subtract 10 from b using the -= operator, and print the updated values of a and b using printf.

The output of the program is:

a = 10, b = 20
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
a = 15, b = 10

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 18 Feb, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read