Learning C
Posted May 13th, 2003 @ 12:41pm by Erik J. Barzeski
I taught Jamie a little bit of C yesterday. Just a little bit. She's a smart girl, so I know she felt like I whipped through some things, but that's part of my plan. Soon she'll begin wondering just what in the hell a pointer is and whether she can do int i = foo(j++);
or something, and she'll look it up. That's where the real learning will occur. I am just a guide.
For whatever reason, I've decided to post the code we created. It is a simple random number guessing game.
#include <stdio.h> #include <stdlib.h> #include <time.h>
#define YES 1 #define NO 0 #define bool int
int randomNumber;
int getRandom(); bool compareGuess(int guess); int getGuess();
int main (int argc, const char * argv[]) { randomNumber = getRandom();
int guess = 0;
do { fflush(stdout); guess = getGuess();
if(guess == 0) printf("Quitter! The number was %d.\n\n", randomNumber); else if(randomNumber < guess) printf("Sorry, too high. Try again.\n"); else if(randomNumber > guess) printf("Sorry, too low. Try again.\n");
} while (randomNumber != guess && guess != 0);
if(guess == randomNumber) { printf("You've guessed correctly!"); printf(" Congratulations!\n"); }
return 0; }
bool compareGuess(int guess) { if(guess == randomNumber) return YES; else return NO; }
int getRandom() { srandom(time(NULL) ); return random()%100; }
int getGuess() { int theGuess; printf("Enter a guess: "); fflush(stdout); scanf("%d", &theGuess);
if(theGuess < 0) { printf("=============\n"); printf("Answer is: %d\n", randomNumber); printf("=============\n"); fflush(stdout); }
return theGuess; }
Posted 13 May 2003 at 1:25pm #
#define bool int? C'mon, she's smart...she can handle C99. 🙂
Posted 13 May 2003 at 1:40pm #
Heh, that was as much for me as for her. She's going to learn Cocoa sooner or later, too, so the point of doing that was to do YES and NO and see BOOL as return type.
Posted 13 May 2003 at 1:40pm #
Congrats to Jamie on joining the cult--soon she will have no life like the rest of us :-). Kidding!!!
Posted 13 May 2003 at 1:55pm #
I never knew about fflush(). Even though I learned C first, I never saw that function call before today. I guess C++ has corrupted us at school. 😉
Posted 14 May 2003 at 2:46am #
Ugh, why the global? If you're going to teach someone, start them right off the bat with good habits!
--Nick
Posted 14 May 2003 at 5:50am #
Why don't you use your compareGuess function?
Posted 14 May 2003 at 8:22am #
Nick: So that I could demonstrate scope.
Oliver: Ha, good point. It was used at one point it just said "your guess is wrong." Then we made the app more complex ("too high" or "too low") and we didn't use it. I could modify the code here and put the "too high" and "too low" code down there, but then I'd be cheating.
Posted 15 May 2003 at 3:49am #
Fair enough ... I figured you were using it to cover scope ... I just tend to do so just long enough to convince them why it's bad, then make them get rid of it before commiting. 🙂
--Nick
Posted 17 May 2003 at 2:53am #
printf("Hello World!")
Barry is full of infinite wisdom. My favorite quote: "Technology is not a destination; it is a journey." Today's journey: Week 1 Day 1 of Sams Teach Yourself C in 21 Days, Sixth Edition. I started learning C from Erik...