C Arithmetic Operators

The arithmetic operators in the C language are used to perform basic mathematical operations. It is used in a C arithmetic expression involving at least two operands and an operator.

An expression is C statement that evaluates to a single value. You can use this value in another C expression or output to the console.

Advertisements

For example,

A = 10;
B = 20;
C = A + B; /* This is an arithmetic expression involving two
Two operands and + operator */

In the above example, the expression A and B will be replaced with 10 + 20, when the program executes and the expression will evaluate to 30. This is assigned to variable C.

Arithmetic Operators

A list of arithmetic operators is given below. The mathematical symbol is actual mathematical operation perform in real world. The C operator is equivalent of that mathematical operation.

Math SymbolC OperatorDescription
++Addition
Subtraction
x*Multiplication
÷/Division
mod%Modulo

Addition

The addition operator perform simple addition on two numbers. It requires two operands of type – integers or real numbers.

For example,

sum = number1 + number2 ;
sum = 10 + 23;
sum = 12.4 + 53.33;

The above are valid C expressions involving addition (+) operator.

Subtraction

The subtraction is similar to addition and perform simple subtraction on integers or real numbers.

For example,

result = number2 – number1;
result = 23.4 – 33.55;
result = 32 – 2;

These are some valid examples of C expression involving the subtraction (-) operator.

Multiplication

The multiplication is the first operation performed when an arithmetic expression is evaluated. We will learn more about operator precedence in future lessons.

Advertisements

The multiplication operator requires at least two operands to perform the multiplication and the result can be output to console and used in another expression.

For example,

C = A * B;
C = 34 * 100;
result = 23.44 * 34.545;

The above expressions are some valid expressions involving multiplication (*) operator.

Division operator

The division requires two operands – a dividend and a divisor. The dividend is divided using divisor and result is a quotient and remainder.

It is assumed that the dividend is always greater than the divisor, but it’s not necessarily true. Sometimes, dividend is a smaller value than divisor, but this does not produce wrong results.

For example,

result = number2 / number1;
result = 56.33/6.55;
result = 32 /22;

These are some valid examples of C expression involving the division (/) operator.

The third example will result in quotient = 1 and remainder = 10, but C division operator during an integer division only output quotient, not remainder.

Modulo Operator

Since, the division operator cannot produce remainder, the modulo operator solve this problem. The modulo operator only produce the remainder when performed on a dividend and a divisor.

For example,

result = number2 % number1; result = 10 % 3 ; 
/* result = 1 */ 
result = 12.44 % 2.20 
/* result = 1.44 */

The mod operation will produce only remainder, there is no quotient or any other outputs.

Example – Arithmetic Operators

/* C Program to demonstrate the Arithmetic Operators */
#include <stdio.h>
int main()
{
    int number1, number2, result;

/* Read Inputs */
    printf("\n\n\n\n\n\n");
    printf(" \t\t\tEnter two numbers\n\n");
    printf("\t\t\tNumber1:");
    scanf("%d",&number1);
    printf("\t\t\tNumber2:");
    scanf("%d",&number2);
    printf("\n\n");

/* Addition */
    result = number1 + number2;
    printf("\t\t\tAddition = %d\n", result);

/* Subtraction */
    result = number1 - number2;
    printf("\t\t\tSubtraction = %d\n", result);

/* Multiplication */
    result = number1 * number2;
    printf("\t\t\tMultiplication = %d\n", result);

/* Division */
    result = number1 / number2;
    printf("\t\t\tDivision = %d\n", result);

/* Modulo */
    result = number1 % number2;
    printf("\t\t\tModulo = %d\n", result);

    getch();
    return 0;
}

Output- Arithmetic Operators

Enter two numbers _
20
10
Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2
Modulo = 0

References

  • Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
  • Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.
  • Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version