This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Arithmetic Operators

The sixth part of the C# Fundamentals tutorial describes the basic arithmetic operators available to the C# programmer. These operators allow general algebraic operations to be carried out against numeric data type values.

Outputting Numeric Values

Before we begin the examination of the arithmetic operators we will take a brief aside. So far in the C# Fundamentals tutorial there has been no discussion of outputting numeric values to the screen. As we start changing variable values, we need to be able to view the results of the operations.

In the first part of the C# Fundamentals tutorial, we created a console application and outputted text using the Console.WriteLine command. This may also be used to output numeric data. For example:

int quantity = 5;
Console.WriteLine(quantity);    // Outputs 5

You can use the Console.WriteLine command to test the examples in the following sections.

Basic Arithmetic Operators

There are five basic arithmetic operators available in C#. These are used for addition (+), subtraction (-), multiplication (*), division (/) and modulus (%). Of these, the first four are available to almost every programming language. Here are some simple examples:

int a = 6;
int b = 3;
int result;

result = a + b;         // result = 9
result = a - b;         // result = 3
result = a * b;         // result = 18
result = a / b;         // result = 2
result = a + b - 1;     // result = 8

It is important to remember that the resultant value of a mathematical operation is subject to the rules of the receiving variable's data type. The result of a division operation may yield a floating point value. However, if assigned to an integer the fractional part will be lost. Equally important, and less obvious, is the effect of an operation performed on several integers and assigned to a non-integer. In this case, the result is calculated as an integer before being implicitly converted. This means that although the resultant value is assigned to a floating point variable, the fractional part is still truncated unless at least one of the values is explicitly converted first. The following examples illustrate this:

int a = 7;
int b = 3;
int integerResult;
float floatResult;

integerResult = a / b;          // integerResult = 2  (truncated)
floatResult = a / b;            // floatResult = 2.0  (truncated)
floatResult = (float)a / b;     // floatResult = 2.33333325

The last of the five arithmetic operators is modulus. This operator yields a value equal to the remainder of an integer division. For example, when ten is divided by three the answer is three and the remainder is one. The modulus operator can be used with all numeric data types.

int a = 10;
int b = 3;
float c = 3.5F;
int integerResult;
float floatResult;

integerResult = a % b;          // integerResult = 1
floatResult = a % b;            // floatResult = 1.0
floatResult = a % c;            // floatResult = 3.0

Increment and Decrement Operators

C# provides two operators that can be used to increase or decrease a variable's value by one. These are the increment operator (++) and decrement operator (--). These operators are known as unary operators because they are applied to a single value. The operators can be placed before the variable to be adjusted (prefix) or after it (postfix).

// All of these commands add one to the variable "a"
a = a + 1;
++a;        // prefix
a++;        // postfix

// All of these commands subtract one from the variable "a"
a = a - 1;

The above examples imply that the prefix and postfix varieties of the operators yield the same results. This is true in the simple scenarios examined so far. However, when the operators are used as a part of a more complex expression there is an important difference. Where the prefix variation is used, the increment or decrement operation is applied before the value of the variable is applied to the rest of the calculation. Where the postfix version is used, the value is changed afterwards.

int a;
int b;

// Prefix.  a is incremented before its value is assigned to b
a = 10;
b = ++a;        //a = 11, b = 11;

// Postfix.  a is incremented after its value is assigned to b
a = 10;
b = a++;        //a = 11, b = 10;

Operator Precedence

The operators discussed in this article may be used together in a single expression. The result of the expression is dependant upon the order in which the operators are applied. This order is determined by the operator precedence, which applies an importance to each operator.

Over the next few articles we will build a table of the operator precedence of C# operators. For the operators described so far, the order of precedence is as follows:

Increment / Decrement Operators
++(postfix) --(postfix) ++(prefix) --(prefix)
Basic Arithmetic Operators
* / % + -

It is important to understand the effect of the operator precedence rules. The following code gives examples of how the importance of an operator affects the end result of an expression:

int a = 10;
int b = 6;
int c = 2;
int result;

// * is applied before +
result = a + b * c;             //result = 22

// / is applied before -
result = a - b / c;             //result = 7

// Order of precedence is *, /, +, -
result = a * b + b / c - 1;     //result = 62

Parentheses Operator

The parentheses operator you to control the order of precedence for operators. By surrounding parts of an expression with parentheses, the precedence of this part of the expression is increased to be of higher importance than items outside of the parentheses. Parentheses may also be nested. The order in which the operators are applied in the previous examples can therefore be modified.

int a = 10;
int b = 6;
int c = 2;
int result;

result = (a + b) * c;             //result = 32

result = (a - b) / c;             //result = 2

result = a * (b + b / (c - 1));   //result = 120
19 August 2006