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 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 | + | 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 | + | 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 117: | Line 117: | ||
</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 | + | 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 [[undefined behavior - C|undefined behavior]] which can do just about anything. |
== Decrement operators == | == 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. | 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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[Category:C programming language]] | [[Category:C programming language]] |