How should I assign 1 value to multiple variables? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
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

31st Oct 2019, 1:45 PM
Phuc Khang
Phuc Khang - avatar
2 Respuestas
+ 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.
31st Oct 2019, 1:53 PM
Nova
Nova - avatar
+ 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.
31st Oct 2019, 1:49 PM
AngryBlackSheep
AngryBlackSheep - avatar