What would happen if you were on a flight, and a la 'Airplane', the pilots die leaving no one with flight experience on board. Now let's say that for some reason the passengers, threatening you with the forks from their in-flight meals, force you to sit at the controls. Would you be able to fly, and, more importantly, land?
I like to think I would. I've logged many hours in flight sims and have even briefly flown the real deal. Pitch, yaw, roll, throttle, flaps, gear. No sudden movements. It's possible we might live, but a little bit of virtual experience does not a pilot make.
What am I talking about? Well, now consider you're a sat down at a computer in front of a pretty development environment. Handy auto-completion makes your life easy, and wizards make short work of all but the toughest task. You've never heard of a pointer in your life, but designers, defaults, and autogenerated code means your program, for the most part, works. Are you a programmer?
Real pilots start small and work their way up. You learn the basics about airspeed, lift, navigation, checklists, weather, and all the other little things that go along with flying to make the effort as riskless as possible. Even fighter pilots start learning with a single rotary engine or gliders. By the time you reach the top of your field, there's no mystery about what lies beneath. You know what's going on when you crank the control column or extend the flaps.
Likewise, programmers have to know what's going on under the hood. Pretty IDEs mask complexity, which is often a good thing. But unless you know what kind of complexity is being masked, you'll always run the risk of augering that plane into the ground, not understanding what went wrong.
Now that I'm done bloviating, what's gotten in to me? I'm just a little giddy after implementing 'atoi' and 'itoa' in straight c. They're just little helper functions that convert numbers to strings and vice versa, nothing big or original I assure you. They don't even handle negative numbers. But still... I find them delightful. One more of those details clinks into place.
int myatoi(char *string)
{
int len = strlen(string);
int retval = 0;
int pow = 1;
for (int i = len-1; i >= 0; i--, pow *= 10)
retval += (string[i] - '0') * pow;
return retval;
}
void reverse(char* string) {
int starti = 0;
int endi = strlen(string) - 1;
char buf;
while (starti < endi) {
buf = string[starti];
string[starti] = string[endi];
string[endi] = buf;
starti++;
endi--;
}
}
char* myitoa(unsigned int number, char *string, int string_length)
{
int sp = 0;
char c;
do {
c = (number % 10) + 48;
number /= 10;
string[sp++] = c;
} while (number > 0 && sp < string_length-1);
string[sp] = ' ';
reverse(string);
return string;
}