Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign single value to multiple variables

Tags:

I want to assign 0 to all declared values in a single statement.

char r, g, b = 0; 

The above only assigns 0 to b but not to the other variables

like image 432
Morrowless Avatar asked Sep 27 '11 07:09

Morrowless


People also ask

How do you assign a single value to multiple variables in Java?

int a, b, c; You can also assign multiple variables to one value: a = b = c = 5; This code will set c to 5 and then set b to the value of c and finally a to the value of b .

Does Python allow you to assign a single value to several variables?

Yes Python allows you to assign a single value to several variables simultaneously.

How do you assign a single value to multiple variables in Golang?

Initialize multiple Variables in Single Line in Golang To assign/initialize multiple Variables in a single line, specify the variables as comma separated on the left side of assignment operator, and the comma separated values on the right side of assignment operator.

Can you assign multiple variables at once in C?

If your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.


2 Answers

You can do it two ways:

char r = 0, g = 0, b = 0; 

or

char r, g, b; r = g = b = 0; 
like image 165
DipSwitch Avatar answered Sep 20 '22 09:09

DipSwitch


Tersest form is:

int r,g,b=g=r=0; 
like image 38
R.. GitHub STOP HELPING ICE Avatar answered Sep 18 '22 09:09

R.. GitHub STOP HELPING ICE