3

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.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
0xAlon
  • 123
  • 1
  • 1
  • 10

6 Answers6

3

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.

  • Usually, the word 'correct' is used instead of 'right' to indicate that a particular expression or statement is syntactically valid. – machine_1 Apr 10 '20 at 10:40
  • 1
    The types involved need to be assignment-compatible, not the same. – pmg Apr 10 '20 at 10:52
  • @pmg I was already at testing your version as you posted the comment. I corrected it. Interesting the implicit cast... But one thing is to care about if using objects of different types: The object which gets assigned needs to be capable to hold the value of the object which is assigned to this one. – RobertS supports Monica Cellio Apr 10 '20 at 11:00
  • Maybe your lecturer is trying to prevent you from assigning pointers and not literals, instead of saying that he told you to never use that type of syntax! – Saadi Toumi Fouad Apr 10 '20 at 12:09
3

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));
pmg
  • 106,608
  • 13
  • 126
  • 198
2

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;
pmg
  • 106,608
  • 13
  • 126
  • 198
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
1

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++);
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • Think about what it means to say “the following are not legal”. There is no rule in the C standard that says you are not allowed to do these. There is a rule that says the C standard does not specify what happens when you do them. But the C standard also does not specify what happens when you open and write to a Unix special device. That also has “undefined behavior” with respect to the C standard, but you would likely not say it is illegal. Some programs need to do it. Can you give a definition of “legal” that allows the latter but not the former? – Eric Postpischil Apr 10 '20 at 16:57
  • @EricPostpischil I changed it to say undefined behavior. To me it makes no difference, since as far as I'm concerned undefined behavior is always to be avoided so I treat it as illegal. But you're correct that it does compile (although it would be far preferable if it didn't). – Tom Karzes Apr 10 '20 at 17:15
1

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

Hadi Pawar
  • 1,090
  • 1
  • 11
  • 23
1

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.

Community
  • 1
  • 1
Gopinath
  • 4,066
  • 1
  • 14
  • 16