C++, 205 179 bytes
int main(){};static int c=1;
#define v(x) A##x
#define u(x) v(x)
#define z u(__LINE__)
#include <cstdio>
class z{public:z(){++c;};~z(){if(c){printf("%d %o %x",--c,c,c);c=0;}}}z;//
(No trailing newline - when copied, the first line of the copy and last line of the original should coincide)
Basically, this works by making a sequence of static variables which, on construction, increment a global variable counter. Then, on destruction, if the counter is not 0, it does all its output and sets the counter to zero.
In order to define a sequence of variables with no name conflicts, we use the macro explained as follows:
#define v(x) A##x //This concatenates the string "A" with the input x.
#define u(x) v(x) //This slows down the preprocessor so it expands __LINE__ rather than yielding A__LINE__ as v(__LINE__) would do.
#define z u(__LINE__)//Gives a name which is unique to each line.
which somewhat relies on the quirks of the string processor. We use z
many times to define classes/variables that will not conflict with each other when copied onto separate lines. Moreover, the definitions which must occur only once are placed on the first line, which is commented out in copies of the code. The #define
and #include
statements don't care that they get repeated, so need no special handling.
This code also features undefined behavior in the statement:
printf("%d %o %x",--c,c,c)
since there are no sequence points, but c is modified and accessed. LLVM 6.0 gives a warning, but compiles it as desired - that --c
evaluates before c
. One could, at the expense of two bytes, add the statement --c;
before the outputs and change --c
in printf
to c
, which would get rid of the warning.
Replaced std::cout
with printf
saving 26 bytes thanks to a suggestion of my brother.
1 01 0x1
? (Includes prefixes) \$\endgroup\$