Can you guess what the following program writes to standard output when run with command-line arguments “8” and “9”, respectively?
#include <iostream>
#include <iterator>
#include <cstdlib>
int main(int argc, char * argv[])
{
if (argc != 2)
{
std::cerr << "Usage: duff-device integer" << std::endl;
return 1;
}
int count( std::atoi(argv[1]) );
int n( (count + 3) / 4 );
std::ostream_iterator<int> int_out(std::cout, " ");
std::ostream_iterator<char const *> str_out(std::cout, " ");
switch(count % 4)
{
case 0: for (*str_out++ = "init"; --n >= 0; *str_out++ = "loop")
{
*int_out++ = count--;
case 3: *int_out++ = count--;
case 2: *int_out++ = count--;
case 1: *int_out++ = count--;
}
}
std::cout << std::endl;
return 0;
}