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 95: | Line 95: | ||
In the expression <code>a / b</code> both a and b are of type <code>int</code>. Therefore the final value is an <code>int</code>. And since 10/20 is 0 (since we cannot represent 0.5 in an <code>int</code>). | In the expression <code>a / b</code> both a and b are of type <code>int</code>. Therefore the final value is an <code>int</code>. And since 10/20 is 0 (since we cannot represent 0.5 in an <code>int</code>). | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[Category:C programming language]] | [[Category:C programming language]] |