C multiple variable assignment

Viewed 1065

I'm first year student in Software Engineering and in my last task I used (chain assignment) multiple assignment of values.

The lecturer claims that it is illegal to assign values in this way.

X = Y = Z;

(The three variables are declared in the beginning of the function).

I would be happy if you can explain to me if the assign is right action and if what the teacher claims is right.

6 Answers

It depends on whether all of the objects were correctly declared and Z was initialized or assigned with an appropriate value before this statement or not.

You can even assign values of objects of different types due to implicit casting, but the object which is getting assigned needs to be capable of holding the value of the object which is assigned to this one.

If Z was initialized or assigned before and all objects were correct declared, then:

X = Y = Z;

is completely correct and legal as it assigns the value held in Z to Y and X - the assignment takes place from right to left.

For example:

int X,Y,Z;      // All objects are declared.

Z = 24;         // `Z` is assigned with the integer value 24.
X = Y = Z;      // `X` and `Y` get assigned by 24 which is the value in `Z`.

Else if Z has an indeterminate value (f.e. if it was not declared with the extern or static keyword or declared at global scope) and you would assign this value to Y and X with that statement. Although the assignment itself would be even then not illegal, if you would use one of the assigned objects the program would give undefined results/output.


Speaking for the multiple assignments only, there is nothing illegal about it, you could even use:

O = P = Q = R = S = T = U = V = W = X = Y = Z;

if all objects used were correctly declared before and Z has an determined value.


The lecturer claims that it is illegal to assign values in this way.

If it is possible, I would ask her/him or your teacher, what s/he meant with that. Maybe there is something specific to your course you should care about.

Almost every expression has a value.
An assignment is an expression with value.
The value of the expression i = 42 is 42.

Assignment associates right to left, so the expression a = b = c is the same as a = (b = c).

So

int i;
double d;
char c;
c = i = d = 42;

Converts 42 to double and assigns to d. The value of this assignment is 42.0.
Then this value is converted to int and assigned to i. Value of this assignment is 42.
Lastly the int value is converted to char and assigned to c.

c = (i = (d = 42));

It's legal, for example:

float x;
int y;
float z = 3.5;
x = y = z;

it's similar to y = z; x = y;. So in this case, z = 3.5, y = 3 and x = 3;

But it will give the warning if you did not initialize the value of z.

int x,y, z;
x = 0;
y = 1;
//z = 2;
x = y = z;

There's nothing illegal about it, provided each variable is only modified once. For example, the following all have well-defined behavior:

x = y = z;
x = y = z = x + 1;
x = y++ + --z;
x += (y = (z += 1)) - 6;

But the following have undefined behavior, and should be avoided:

x = y = x = z;
x = x++;
x = y++ + y++
x = func(y++, y++);

Together with right-associativity of evaluation, and assuming non-volatile a, b, and c, it means that a = b = c is equivalent to a = (b = c), and again equivalent to b = c; a = b

Assigning values to multiple variables in a single line is perfectly alright and is allowed by C programming language.

However, the use of this style is usually discouraged because it may cause undesirable side-effects in some cases.

The expectation of Mathematical equation X = Y = Z may be:

Y = Z and X = Z (I.E., Assign the value of Z to both X and Y)

But C language treats the multiple assignments like a chain, like this:

In C, "X = Y = Z" means that the value of Z must be first assigned to Y, and then the value of Y must be assigned to X.

Here is a sample program to see how the multiple assignments in single line work:

#include <stdio.h>

int main() {   
    int n;
    int X, Y, Z;
    
    printf("Enter an integer: ");  
    scanf("%d", &n);

    printf("You entered: %d\n", n);
    
    printf("Performing multiple assignments:\n X = Y = Z = n++ \n");
    X = Y = Z = n++;
    printf("n = %d\n", n);
    printf("X = %d \n", X);
    printf("Y = %d \n", Y);
    printf("Z = %d \n", Z);

    return 0;
}

Output:

Enter an integer: 100                                                                                                                                                                      
You entered: 100                                                                                                                                                                            
Performing multiple assignments:                                                                                                                                                            
 X = Y = Z = n++                                                                                                                                                                            
n = 101
X = 100
Y = 100
Z = 100

Points to note in the above example:

The line X = Y = Z = n++ got processed like this:

Step 1: Assign the value of n (100) to Z

Step 2: Increment the value of n (n becomes 101)

Step 3: Assign the value of Z to Y (Y becomes 100)

Step 4: Assign the value of Y to X (X becomes 100)

Conclusion:

'Multiple assignments in single line' is a supported style. It has its benefits.

However, if the programmer is not aware of the sequence of operations involved in the multiple assignments statement, then it can lead to inconsistency between the expectation of the person who reads the program and the actual result of the execution.

To avoid this scenario, the multiple assignments are discouraged.

Related