Subscribe Us

Arithmetic Operator in C programming language - smartduniya

artithmatic operator in c

Introduction of Arithmetic Operator in C programming language

C language has common arithmetic operators such as +,-,* , / and modulus operator %. The modulus operator (%) returns the remainder of integer division calculation.  Note that the modulus operator cannot be applied to a double or float.

C Arithmetic Operators Precedence

The C arithmetic operators have precedence rules that are similar the rule in math.  The + and - operators have the same precedence. The precedence of *, / and % operators are higher than the + and - operators . The C arithmetic associate left to right.

Here is a simple program to demonstrate C arithmetic operators:

 

#include <stdio.h>

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);

return 0;

}

write the above code on your C compiler and run the program you will get the below  result . 

 

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

Post a Comment

0 Comments