Subscribe to
Posts
Comments
NSLog(); Header Image

Learning C

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;
}

9 Responses to "Learning C"

  1. #define bool int? C'mon, she's smart...she can handle C99. 🙂

  2. 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.

  3. Congrats to Jamie on joining the cult--soon she will have no life like the rest of us :-). Kidding!!!

  4. 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. 😉

  5. Ugh, why the global? If you're going to teach someone, start them right off the bat with good habits!

    --Nick

  6. Why don't you use your compareGuess function?

  7. 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.

  8. 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

  9. 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...


Trackback URI | Comments RSS

Leave a Reply

Please abide by the comment policy. Valid HTML includes: <blockquote><p>, <em>, <strong>, <ul>, <ol>, and <a href>. Please use the "Quote Me" functionality to quote comments.