5 ms·
Generated C: #include <stdio.h> char mem[30000] = { }; #define OUTPUTS 0 char outputs[OUTPUTS] = { }; int main() { char* ptr = mem +
by NilsIRL 7y ago
Generated C:
#include <stdio.h>
char mem[30000] = { };
#define OUTPUTS 0
char outputs[OUTPUTS] = { };
int main() {
char* ptr = mem + 0;
fwrite(outputs, sizeof(char), OUTPUTS, stdout);
ptr[2] = getchar();
++ptr[2];
while (ptr[2]) {
ptr[2] -= 11;
while (ptr[2]) {
ptr[2] -= 22;
while (ptr[2]) {
++ptr[3];
--ptr[1];
while (ptr[1]) {
++*ptr;
++ptr[1];
}
ptr[2] = 0;
}
ptr[2] = 0;
}
ptr[1] = 0;
ptr[1] += ptr[3] * 1;
ptr[3] = 0;
ptr[2] = getchar();
++ptr[2];
}
ptr[1] = 0;
ptr[3] += *ptr * 1;
ptr[2] += *ptr * 1;
*ptr = 0;
*ptr += ptr[3] * 1;
ptr[3] = 0;
++ptr[1];
while (ptr[2]) {
--ptr[1];
while (ptr[2]) {
ptr[3] += 10;
while (ptr[2]) {
--ptr[2];
--ptr[3];
while (ptr[3]) {
++ptr[4];
ptr += 3;
}
while (ptr[4]) {
++ptr[4];
ptr[3] += ptr[4] * 1;
ptr[4] = 0;
++ptr[5];
ptr += 3;
}
ptr -= 3;
}
ptr[3] = 8;
ptr[2] += ptr[3] * 6;
ptr[3] = 0;
ptr[2] += ptr[4] * 1;
ptr[4] = 0;
ptr[3] += ptr[5] * 1;
ptr[5] = 0;
++ptr;
}
++ptr;
}
while (ptr[1]) {
--ptr[1];
ptr[3] += 8;
ptr[2] += ptr[3] * 6;
ptr[3] = 0;
ptr += 2;
}
while (ptr[0]) {
putchar(ptr[0]);
*ptr = 0;
--ptr;
}
ptr[1] += 10;
putchar(ptr[1]);
return 0;
}
- markus_zhang 7y agomy eyes!
- zabzonk 7y agoNote that: #define OUTPUTS 0 char outputs[OUTPUTS] = { }; is not valid C (or C++) - zero length arrays are not a thing in either language,
- jacquesm 7y ago(base) jam@jam-XPS-8500:~/svn/src/music/mp3tomidi$ cc -c -Wall x.c (base) jam@jam-XPS-8500:~/svn/src/music/mp3tomidi$ cat x.c #define OUTPUTS 0 char outputs[OUTPUTS] = { }; Looks like you're wrong about that; at least for GNU C. It's not official C but if it works and generates no warnings it will be used too. Long HN thread about this subject: https://news.ycombinator.com/item?id=11674374 https://news.ycombinator.com/item?id=11674374 Zero length arrays are one of those things that C is renowned and notorious for: something that is useful at times but unpredictable because it is flirting with undefined behavior. I usually compare C with a Formula 1 race car. It works well as long as you don't cut too many corners and when you do you end up plastered all over the road.
- zabzonk 7y ago[neilb@Miranda temp]$ gcc -Wall -Wextra -pedantic a.c a.c:2:6: warning: ISO C forbids zero-size array 'outputs' [-Wpedantic] char outputs[OUTPUTS] = { };
- jacquesm 7y agoYes, the -pedantic is what will trigger that. See linked post. But that's not on by default (fortunately!). ISO C is so restrictive ;)
- zabzonk 7y ago> (fortunately!) Unfortunately. All warnings and errors should be on by default. That they are not is one of the things about GCC that really pisses me off.
- jacquesm 7y agoStill not getting the joke. Yes, they should be on, in fact GCC should not do this, 'extensions' to a language that invite undefined behavior are not in line with solid software development practices. Neither is brainfuck...