6 ms·
> nobody really does that, because that would be kind of insane Yeah. That's why people don't do things in C. It's more like most C programmers probably weren'
by bachmeier 5y ago
> nobody really does that, because that would be kind of insane
Yeah. That's why people don't do things in C. It's more like most C programmers probably weren't aware of this. After your comment, we'll start to see C codebases everywhere with that.
- colejohnson66 5y agoThis “trick” has been know at least as far back as 2008: https://stackoverflow.com/questions/381542/with-arrays-why-is-it-the-case-that-a5-5a https://stackoverflow.com/questions/381542/with-arrays-why-i...
- lifthrasiir 5y agoOne of the very first IOCCC winners used the trick in 1984 (1984/anonymous): int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\ o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
- jakeva 5y agowhat does this program do? i compiled it and ran it and got no output. i hope i haven't fork bombed myself or something
- lifthrasiir 5y agoPrints "hello, world!", assuming sizeof(int) == sizeof(char*) (and the same alignments and ABI, etc).
- jfrunyon 5y agoI think this is equivalent to: int i; int main(){ for( ; i["]<i;++i){--i;}"]; // loop until i == 14? (the NUL byte on the end) read('-'-'-',i+++"hello, world!\n",'/'/'/') // read(0, <one byte of "hello, world!\n" at a time>, 1) ) {}; } int read(j,i,p){ write(j/p+p,i---j,i/i); // write(0/1+1, i-- - 0, 1) --> write(1, i, 1) --> write a byte to STDOUT }
- tialaramex 5y agoIt's actually spelled out in K&R in more technical language: "The array subscripting operation is defined so that E1[E2] is identical to *(E1+E2). Therefore, despite its asymmetrical appearance, subscripting is a commutative operation." (my emphasis)
- LeifCarrotson 5y agoMost programmers probably weren't aware of this, but no true Scottish C programmer would be unaware of it. If you want to manipulate memory directly - which is risky though sometimes useful - C is one of the best languages in which to do it. Memory addresses are numbers, and C will let you work with those numbers in whatever way you want: add, subtract, multiply, divide... and if you didn't shudder at the suggestion of dividing a pointer because there are very, very few reasons to do so then C is not the language for you! If you don't want to manipulate memory directly, you probably shouldn't be using C; stick with a nice garbage-collected, type-safe, object-oriented, cross-platform language. If you do want to manipulate memory directly, but you want more guarantees on what you can do with pointers, try Rust.
- bluejekyll 5y agoJust wanted to clarify that Rust allows for the same manipulation as C, it just all has to happen in the context of an unsafe code block. I think your comment might be taken to imply that Rust isn’t as capable as C in that regard.
- deleted 5y ago[deleted]
- pjmlp 5y agoPlenty of languages allow for the same memory handling capabilities, including BASIC.
- dahfizz 5y agoI learned of this in college. Most C programmers know how C arrays work.
- junon 5y agoEh? Says who? Every C programmer I've ever met knows about this. It's basic C.
- jart 5y agoYeah you have to do something like this if you want to truly raise eyebrows. /\ */ best c comment *\ /
- Zababa 5y agoOne trick that I like is replacing { and } with <% and %>.
- junon 5y agoWow, a new C trick I didn't know about. What's the history here? Also, gross.
- turminal 5y agoOn some ancient systems { and } don't exist. So an alternarive made of more common characters is provided. There is also a two-character combination for [ and ] and a three character combination for |, & and some other characters. The feature is called digraphs/trigraphs and is disabled on most modern compilers by default.
- junon 5y agoAh okay I knew about that whole thing, I didn't think C had so many of them.
- Zababa 5y ago> The feature is called digraphs/trigraphs and is disabled on most modern compilers by default. Digraphs and trigraphs are treated differently. <% and %> are fine on GGC and Clang, but ??< and ??> are disabled by default. Both need the -trigraphs option.