summaryrefslogtreecommitdiff
path: root/reference/C/CONCEPT/assignment.html
blob: 5985d5ba7a6e70d0c8ac7b2811ddb72bd2d653a0 (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
85
86
87
88
89
90
91
<title>Assignment operators.</title>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Assignment operators.</h1>
</center>
<hr>

C has the following assignement operators:
<pre>
	=   +=   -=   *=   /=   %=   <<=   >>=   &=   ^=   |=
</pre>
= is the obvious one. It takes the result of the expression on the right 
and assigns it to the variable on the left (lvalue).
<pre>
	main ()
        {
	  int a, b=4, c=10;
	  a = b + c;
	}
</pre>
I know this is stating the obvious, but:
<ol>
<li><b>b</b> is assigned the value 4
<li><b>c</b> is assigned the value 10
<li><b>a</b> is assigned the result of b plus C (14).
</ol>

<hr>
All the other operators act in a simular way. If you find yourself 
coding the example on the left, it could be changed to the example on 
the right.
<pre>
	main ()				main()
	{				{
	  int a=4;			  int a=4;
	  a = a * 4;			  a *= 4;
	}				}
</pre>
The rule is as follows:
<p>
The lvalue is multiplied by the rvalue and the result assigned to the 
lvalue.
<p>
Here is another example.
<pre>
	main()				main()
	{				{
	    int a=10, b=2;	            int a=10, b=2;
	    a /= b * 5;		            a = a / (b*5);
	}				}
</pre>
Again both preduce the same answer (1).
<p>
<hr>

<img src=../../GRAPHICS/whiteball.gif>
<a href="inc_dec.html">Increment (++) and Decrement (--)</a><br>

<img src=../../GRAPHICS/whiteball.gif>
<a href="bit_shift.html">Bit shifting ( >>= <<= )</a><br>

<img src=../../GRAPHICS/whiteball.gif>
<a href="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>