blob: dd8216b39c14338626c0ba4e1b0cb06c63bb8e82 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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>
|