Hacker News new | past | comments | ask | show | jobs | submit login

Excerpt:

"Write snprintf in C. For those who haven’t used C before, snprintf is a function that produces formatted output based on an input string and a variable number of arguments. Doing it in C forces you to deal with constraints you may not have had to deal with in a higher-level language. Don’t skip out on writing unit tests! (And don’t bother with floating-point numbers; just handle %d and %s.)

const size_t bufferLength = 128;

char buffer[bufferLength];

snprintf(buffer, bufferLength, "%s %d %s", "first", 2, "last");

assert(0 == strcmp(buffer, "first 2 last"));

Write snprintf in assembly…for the exact same reason. Pretty much no one programs in assembly any more, and that’s generally a good thing, but this will (a) force you to learn a new and very suboptimal language, (b) get you to learn a little about your CPU2, and (c) help you later on if you ever need to debug a compiled program without debug info. Bonus points if you can get your assembly version to work correctly with C."




snprintf is merely a template engine. The parsing part is in sscanf. That's where it gets interesting.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: