From WikiChip
Editing c/basic arithmetics

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{c title|Basic Arithmetics}}
+
{{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.
 
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 ==
Once a variable has been {{C|Variables#Variable Declaration|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.
+
Once a variable has been [[Variables - C#Variable Declaration|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:
 
C provides the following basic arithmetic operators:
Line 71: Line 71:
  
 
== Operations of mixed types ==
 
== 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 {{C|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.
+
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 - C|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:
 
Consider the following program:
Line 116: Line 116:
 
groupA  = groupA + 1;
 
groupA  = groupA + 1;
 
</source>
 
</source>
 
It is important to note that the object can only be modified once in a single statement. Expressions such as <code>x = x++;</code> result in {{C|undefined behavior}} which can do just about anything.
 
 
== Decrement operators ==
 
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.
 
 
<source lang="C">
 
#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;
 
}
 
</source>
 
 
Which will produce the following result:
 
x = 4; y = 9; z = 14;
 
 
== Type conversions ==
 
{{main|c/type conversion|l1=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:
 
 
<source lang="C">
 
#include <stdio.h>
 
int main()
 
{
 
    double f = 3.14;
 
    printf("=> %d\n", (int)(f / 2));
 
    return 0;
 
}
 
</source>
 
 
Produces the following output: <code>=> 1</code>. In the example above, we've converted a <code>double</code> to an <code>int</code> by casting it. The syntax for the cast operator is:
 
(<type>)<expression>
 
 
For example: <code>a / (double)x</code> will cast the variable x into a <code>double</code> before performing the division.
 
 
<source lang="C">
 
#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;
 
}
 
</source>
 
 
Which produces the following output:
 
a/b = 0.000000
 
a/(double)b = 0.625000
 
  
 
[[Category:C programming language]]
 
[[Category:C programming language]]

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)