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 range 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 {{C|undefined behavior}} which can do just about anything.
+
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 ==
Line 137: Line 137:
  
 
== Type conversions ==
 
== Type conversions ==
{{main|c/type conversion|l1=type conversion}}
+
{{main|type conversion - C|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:
 
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:
  

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)