From WikiChip
Difference between revisions of "c/basic arithmetics"
< c

(Increment operators)
Line 97: Line 97:
  
 
== Increment operators ==
 
== Increment operators ==
The '''++''' ('''increment''') operator adds 1 to the value of a modifiable arithmetic data object.
+
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:
 +
<source lang="C">total = ++groupA;</source>
 +
 
 +
The statement above is similar to the following line
 +
<source lang="C">
 +
groupA  = groupA + 1;
 +
total = groupA;
 +
</source>
 +
 
 +
Likewise, the code
 +
<source lang="C">total = groupA++;</source>
 +
 
 +
behaves similar to
 +
<source lang="C">
 +
total = groupA;
 +
groupA  = groupA + 1;
 +
</source>
  
 
[[Category:C programming language]]
 
[[Category:C programming language]]

Revision as of 20:13, 27 December 2013

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

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

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

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;