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

(Created page with "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. ...")
 
Line 1: Line 1:
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.
+
{{C Guide}}
 +
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 ==
 
== Expressions & Operators ==
Line 20: Line 21:
 
| % || Remainder of a division of 1st operand from 2nd
 
| % || Remainder of a division of 1st operand from 2nd
 
|}
 
|}
 +
 +
One very simple expression can be <code>3 * 5</code>, another is <code>3 + 6 * 2</code>. Math operations follow the order of operations, however, parentheses can also be used to change it. For example <code>((4 + 6 + 2) * 4)</code>
 +
 +
For example, a basic program might look like this:
 +
 +
<source lang="C">
 +
#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;
 +
}
 +
</source>
 +
 +
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
 +
 +
[[Category:C programming language]]

Revision as of 16:37, 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