Home C Programming Language C Operators

Arithmetic Operators

C programming language supports almost common arithmetic operator such as +,-,* and modulus operator %. Modulus operator (%) returns the remainder of integer division calculation. The operators have precedence rules which are the same rule in math.

Here is a C program demonstrate arithmetic operators:

#include <stdio.h>
/* a program demonstrates C arithmetic operators */
void main(){
    int x = 10, y = 20;
    
    printf("x = %d\n",x);
    printf("y = %d\n",y);
    /* demonstrate = operator + */
    y = y + x; 
    printf("y = y + x; y = %d\n",y);
 
    /* demonstrate - operator */
    y = y - 2;
    printf("y = y - 2; y = %d\n",y);
    /* demonstrate * operator */
    y = y * 5;
    printf("y = y * 5; y = %d\n",y);
 
    /* demonstrate / operator */
    y = y / 5;
    printf("y = y / 5; y = %d\n",y);
 
    /* demonstrate modulus operator % */
    int remainder = 0;
    remainder = y %3;
 
    printf("remainder = y %% 3; remainder = %d\n",remainder);
 
    /* keep console screen until a key stroke */
    char key;
    scanf(&key);
}

And here is the output

x = 10
y = 20
y = y + x; y = 30
y = y - 2; y = 28
y = y * 5; y = 140
y = y / 5; y = 28
remainder = y % 3; remainder = 1