From WikiChip
Basic Arithmetics - C
< c
Revision as of 06:20, 4 January 2015 by Inject (talk | contribs) (Type conversions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Just about every program is going to need to do some basic arithmetics - the ability to manipulate values using basic math operations such as addition and subtraction. The C programming language provides a set of operations to perform such basic operations.

Expressions & Operators[edit]

Once a variable has been declared and assigned some value, it is possible to use that variable in various arithmetic operations. Expressions, among other things, is the combination of arithmetic operators, values, and variables, that are combined together to produce a single value.

C provides the following basic arithmetic operators:

Operator Description
+ Addition of both operands
- Subtraction of 2nd operand from 1st
* Multiplication of both operands
/ Division of 1st operand from 2nd
 % Remainder of a division of 1st operand from 2nd

One very simple expression can be 3 * 5, another is 3 + 6 * 2. Math operations follow the order of operations, however, parentheses can also be used to change it. For example ((4 + 6 + 2) * 4)

For example, a basic program might look like this:

#include <stdio.h>
int main()
{
    int a, b;
    printf("Please enter two integer, comma-separated: ");
    scanf("%d,%d", &a, &b);
    printf("%d + %d = %d\n", a, b, a + b);
    printf("%d - %d = %d\n", a, b, a - b);
    printf("%d * %d = %d\n", a, b, a * b);
    printf("%d / %d = %d\n", a, b, a / b);
    printf("%d %% %d = %d\n", a, b, a % b);
    return 0;
}

One possible output for the program above is:

Please enter two integer, comma-separated: 23,5
23 + 5 = 28
23 - 5 = 18
23 * 5 = 115
23 / 5 = 4
23 % 5 = 3

The math expressions above can be assigned to and mixed with other variables, for example, we can perform (c % ((a + 4) * b)) if we had three variables of type int. Consider the example below:

#include <stdio.h>
int main()
{
    int length, width, area;
    printf("Please enter the length and width of a rectangle, comma-separated: ");
    scanf("%d,%d", &length, &width);
    
    area = length * width;
    printf("The area of the rectangle is %d\n", area);
    return 0;
}

One possible output is:

Please enter the length and width of a rectangle, comma-separated: 5,11
The area of the rectangle is 55

Operations of mixed types[edit]

Up until now we've only shown examples that have expressions with only homogeneous types. For example the addition of two int types or multiplication of two int types. Expressions, however, can involve values of different types. When this happen, a protocol for converting them to a common type is invoked, known as the usual arithmetic conversions. Depending on the type of the operand, and expression it generally converted to the type of the bigger range type among the two. This concept sometimes produce confusing outputs for new programmers.

Consider the following program:

#include <stdio.h>
int main()
{
    int a = 10, b = 20;
    float c;

    c = a / b;
    printf("%d / %d = %f\n", a, b, c);
    return 0;
}

Executing the program above will produce the following out:

10 / 20 = 0.000000

It might not be exactly what you were expecting. Let's zoom in on the cause:

c = a / b;

In the expression a / b both a and b are of type int. Therefore the final value is an int. And since 10/20 is 0 (since we cannot represent 0.5 in an int).

Increment operators[edit]

The ++ (increment) operator adds 1 to the value of a modifiable arithmetic or pointer data object. The operand receives the result of the increment operation. The operator can be placed before or after the operand. If the operator is applied before the operand, the operand is incremented and the result is used immediately in the expression. If the operator is applied after the operand, the value of the operand is used in the expression before the operand is incremented. When the operator is placed before the operand, it is called a pre-increment operator; when it is placed after the operand, is it called a post-increment operator.

Consider the code below:

total = ++groupA;

The statement above is similar to the following line

groupA  = groupA + 1;
total = groupA;

Likewise, the code

total = groupA++;

behaves similar to

total = groupA;
groupA  = groupA + 1;

It is important to note that the object can only be modified once in a single statement. Expressions such as x = x++; result in undefined behavior which can do just about anything.

Decrement operators[edit]

Just like the increment operator, an operator to subtract a value also exist. The ++ (decrement) operator subtracts 1 to the value of a modifiable arithmetic or pointer data object. The operand receives the result of the decrement operation. The operator can be placed before or after the operand. If the operator is applied before the operand, the operand is decremented and the result is used immediately in the expression. If the operator is applied after the operand, the value of the operand is used in the expression before the operand is decremented. When the operator is placed before the operand, it is called a pre-decrement operator; when it is placed after the operand, is it called a post-decrement operator.

#include <stdio.h>
int main()
{
    int x = 5, y = 10, z;
    z = --x + y--;
    printf("x = %d; y = %d; z = %d;\n", x, y, z);
    return 0;
}

Which will produce the following result:

x = 4; y = 9; z = 14;

Type conversions[edit]

Main article: type conversion

While many conversions are automatically performed when compatible types are assigned and operated on, some conversions are a bit more problematic because they may result in truncation or loss of value. These type of conversion are known as type-casting. Consider the following example:

#include <stdio.h>
int main()
{
    double f = 3.14;
    printf("=> %d\n", (int)(f / 2));
    return 0;
}

Produces the following output: => 1. In the example above, we've converted a double to an int by casting it. The syntax for the cast operator is:

(<type>)<expression>

For example: a / (double)x will cast the variable x into a double before performing the division.

#include <stdio.h>
int main()
{
    double o1, o2;
    int a = 5, b = 8;
    
    o1 = a / b;
    o2 = a / (double)b;
    
    printf("a/b = %f\n", o1);
    printf("a/(double)b = %f\n", o2);
    return 0;
}

Which produces the following output:

a/b = 0.000000
a/(double)b = 0.625000