C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

As you can see from the order of operators table, the assignment operator has precedence and
associativity, as do the rest of the operators. Assignment has very low priority in the table, and it
associates from right to left.


The right-to-left associativity lets you perform an interesting operation: You can assign a value to
more than one variable in the same expression. To assign the value of 9 to 10 different variables, you
could do this:


Click here to view code image


a = 9; b = 9; c = 9; d = 9; e = 9; f = 9; g = 9; h = 9; i = 9; j = 9;

but this is easier:


Click here to view code image


a = b = c = d = e = f = g = h = i = j = 9;

Because of the right-to-left associativity, C first assigns the 9 to j, then puts the 9 in i, and so on.


Note

C doesn’t initialize variables for you. If you wanted to put 0 in a bunch of variables, a
multiple assignment would do it for you.

Every C expression produces a value. The expression j = 9; does put a 9 in j, but it also results
in a completed value of 9 , which is available to store somewhere else, if needed. The fact that every
assignment results in an expression lets you do things like this that you can’t always do in other
programming languages:


Click here to view code image


a = 5 * (b = 2); /* Puts a 2 in b and a 10 in a */

Here’s one last program example that uses assignments, operators, and parentheses to change the
order of operators:


Click here to view code image


// Example program #3 from Chapter 9 of Absolute Beginner's Guide to
// C, 3rd Edition
// File Chapter9ex3.c
/* This program calculates the average of four grades and also does
some other basic math. */
#include <stdio.h>
main()
{

int grade1, grade2, grade3, grade4;
float averageGrade, gradeDelta, percentDiff;
Free download pdf