4 ms·
This is interesting and timely since just recently I attempted to make the same journey in the name of creating a tiny obfuscated C program[0]. However, right a
by lelandbatey 12y ago
This is interesting and timely since just recently I attempted to make the same journey in the name of creating a tiny obfuscated C program[0]. However, right at the part where the author says "you might start thinking that you no longer care how printf works" is exactly where I stopped caring.
In the end, I did find out a way to write to stdout without calling putchar or including stdio directly, but there's still some mystery in the call to write:
// Printing to stdout without stdio or putchar
// Originally adapted from here:
// http://stackoverflow.com/a/14296280
void print_char(char item, int len)
{
for (;len; --len)
{
write(1, &item, 1);
}
}
[0] - https://github.com/lelandbatey/tiny_tree_printer https://github.com/lelandbatey/tiny_tree_printer
- cbd1984 12y agoHow is that function mysterious? It just writes the same character to stdout len times. The only mystery is how anyone thought that would be efficient.
- lelandbatey 12y agoWhat I mean by mysterious is that the syscall is hidden in the write statement, vs hidden in the printf statement. Also, I wrote the above function (actually a much smaller version) as part of a program that would print a binary tree of any given height[0]. It wasn't meant to be efficient, it was meant to be small, since the total size of the program was 777 bytes (later reduced to 505 bytes). [0] - http://lelandbatey.com/posts/2014/09/binary-tree-printer/ http://lelandbatey.com/posts/2014/09/binary-tree-printer/