The arithmetic operators in 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 console.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]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.
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 Symbol||C Operator||Description|
The addition operator perform simple addition on two numbers. It requires two operands of type – integers or real numbers.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]sum = number1 + number2 ;
sum = 10 + 23;
sum = 12.4 + 53.33;
The above are valid C expressions involving addition (+) operator.
The subtraction is similar to addition and perform simple subtraction on integers or real numbers.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]result = number2 – number1;
result = 23.4 – 33.55;
result = 32 – 2;[/cc]
These are some valid examples of C expression involving the subtraction (-) operator.
The multiplication is the first operation performed when an arithmetic expression is evaluated. We will learn more about operator precedence in future lessons.
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.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]C = A * B;
C = 34 * 100;
result = 23.44 * 34.545;
The above expressions are some valid expressions involving multiplication (*) 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.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]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.
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.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]result = number2 % number1;
result = 10 % 3 ; /* result = 1 */
result = 12.44 % 2.20 /* result = 1.44 */[/cc]
The mod operation will produce only remainder, there is no quotient or any other outputs.
Example – Arithmetic Operators
[cc lang=”c” tab_size=”4″ line_numbers=”off”]/* C Program to demonstrate the Arithmetic Operators */
int number1, number2, result;
/* Read Inputs */
printf(” \t\t\tEnter two numbers\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);
Output- Arithmetic Operators
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.