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

(Expressions & Operators)
Line 49: Line 49:
 
  23 / 5 = 4
 
  23 / 5 = 4
 
  23 % 5 = 3
 
  23 % 5 = 3
 +
 +
The math expressions above can be assigned to and mixed with other variables, for example, we can perform <code>(c % ((a + 4) * b))</code> if we had three variables of type int. Consider the example below:
 +
 +
<source lang="C">
 +
#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;
 +
}
 +
</source>
 +
 +
One possible output is:
 +
Please enter the length and width of a rectangle, comma-separated: 5,11
 +
The area of the rectangle is 55
 +
  
 
[[Category:C programming language]]
 
[[Category:C programming language]]

Revision as of 17:08, 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