0
How should I assign 1 value to multiple variables?
I only know 1 way to do that, which is to put all the variables into 1 declaration and make them equal to a value: int a, b, c,... = (Number); But when I put 5 variables into one declaration and then print out, the values of some variables changes. So, did I do something wrong and could I fix it? https://code.sololearn.com/cBa6i81BwUEh/?ref=app https://code.sololearn.com/cBa6i81BwUEh/?ref=app
2 Answers
+ 10
First declare all variables and then assign them value on the next line.
Example:
int a, b, c, d, e; // variable declaration
a = b = c = d = e = 10; // assign single value to multiple variables in one line
What you did is just assigning the value to the last variable. And while printing some garbage value is being printed.
+ 3
Iâm afraid the way you are doing it only assigns a value to f (the 5th variable). The other variables when printed will then give random numbers, because they donât have a value yet.