How to assign a single value to several variables in one line with C++

2 Answers

0 votes
#include <iostream>

int main() {
    int a, b, c, d;
    
    a = b = c = d = 8;    

    std::cout << a << " " << b << " " << c << " " << d << "\n";
}



/*
run:

8 8 8 8

*/

 



answered Jul 9, 2024 by avibootz
0 votes
#include <iostream>
 
int main() {
    int a, b, c, d = c = b = a = 8;

    std::cout << a << " " << b << " " << c << " " << d << "\n";
}
 
 
 
/*
run:
 
8 8 8 8
 
*/

 



answered Jul 14, 2025 by avibootz
...