summaryrefslogtreecommitdiff
path: root/reference/C/CONCEPT/inc_dec.html
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/CONCEPT/inc_dec.html')
-rw-r--r--reference/C/CONCEPT/inc_dec.html84
1 files changed, 84 insertions, 0 deletions
diff --git a/reference/C/CONCEPT/inc_dec.html b/reference/C/CONCEPT/inc_dec.html
new file mode 100644
index 0000000..dd8216b
--- /dev/null
+++ b/reference/C/CONCEPT/inc_dec.html
@@ -0,0 +1,84 @@
+<title>Increment and decrement.</title>
+<body bgcolor="#ffffcc">
+<hr>
+<center>
+<h1>Increment and decrement.</h1>
+</center>
+<hr>
+
+The traditional method of incrementing numbers is by coding something like:
+<pre>
+ a = a + 1;
+</pre>
+Within C, this syntax is valid but you can also use the ++ operator to perform
+the same function.
+<pre>
+ a++;
+</pre>
+will also add 1 to the value of <b>a</b>.
+By using a simular syntax you can also decrement a variable as shown below.
+<pre>
+ a--;
+</pre>
+These operators can be placed as a prefix or post fix as below:
+<pre>
+ a++; ++a;
+</pre>
+When used on their own (as above) the prefix and postfix have the same effect
+BUT within an expression there is a subtle difference....<p>
+
+<ol>
+<li>Prefix notation will increment the variable BEFORE the
+expression is evaluated.
+<li>Postfix notation will increment AFTER the expression evaluation.
+</ol>
+
+Here is an example:
+<pre>
+ main() main()
+ { {
+ int a=1; int a=1;
+ printf(" a is %d", ++a); printf(" a is %d", a++);
+ } }
+
+</pre>
+
+In both examples, the final value of <b>a</b> will be 2. BUT the first
+example will print 2 and the second will print 1.
+
+<hr>
+<img src=../../GRAPHICS/computer.gif>
+<a href="../EXAMPLES/inc_dec.c"> Example program.</a><br>
+
+<img src=../../GRAPHICS/whiteball.gif>
+<a href="../CONCEPT/expressions.html">Other operators.</a><br>
+
+<img src=../../GRAPHICS/whiteball.gif>
+<a href="../CONCEPT/precedence.html">Operator precedence table.</a>
+<hr>
+<p>
+<center>
+<table border=2 width=80% bgcolor=ivory>
+<tr align=center>
+<td width=25%>
+<a href="../cref.html">Top</a>
+</td><td width=25%>
+<a href="../master_index.html">Master Index</a>
+</td><td width=25%>
+<a href="../SYNTAX/keywords.html">Keywords</a>
+</td><td width=25%>
+<a href="../FUNCTIONS/funcref.htm">Functions</a>
+</td>
+</tr>
+</table>
+</center>
+<p>
+
+<hr>
+<address>Martin Leslie
+<script language="JavaScript">
+<!-- //
+document.write(document.lastModified);
+// -->
+</script>
+</address>