Number guessing game in C This is just a little number guessing game. I know it's simple but I am a newbie in C and I think such a little example can be enough for finding simple design flaws that can than be eliminated.

If you know how parts of the code can be written more beautiful and more safe, please let me know!

/*
 * In this program the user has to guess a number between 1 and 99.
 * He has six tries.
 */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int generate_random_number(int lower_range, int upper_range) 
{
    srand(time(0));
    return (rand() % (upper_range - lower_range + 1)) + lower_range; 
}

// this function stops the program until useless input is entered 
void pause()
{
    int useless;
    scanf("%i", &amp;useless);
}

int main()
{   
    // generate random number
    const int lower_range = 1;
    const int upper_range = 99;
    const int random_number = generate_random_number(lower_range, upper_range);

    // start of game
    printf("You have to guess a number between 1 and 99.\n");
    printf("You have six tries.\n");
    const int tries = 6;
    for (int i = 0; i &lt; tries; i++)
    {
        printf("&gt; ");
        int guess;
        scanf("%i", &amp;guess);

        if (guess &gt; random_number)
        {
            printf("The secret number is smaller.\n");
        }
        else if (guess &lt; random_number)
        {
            printf("The secret number is higher\n");
        }
        else if (guess == random_number)
        {
            printf("You found the secret number!\n");
            pause();
            return 0;
        }
    }

    printf("You haven't found the secret number.\n");
    printf("The secret number was %i.\n", random_number);
    pause();
}
This looks quite good code.  You've avoided many of the common mistakes.

Here are the things I spotted as I walked through.  Most of them are quite minor; the only serious issue is the handling of invalid user input.




srand(time(0));



Don't do this every time you want a random number - a random number generator is intended to be seeded just once.  So move the srand() call to the beginning of `main().

(Yes, I know this program calls generate_random_number() only once.  But we shouldn't need to have that knowledge, and we might want to change that behaviour, or to re-use this function in another program.)




// this function stops the program until useless input is entered



What's the point?  That just annoys the user, who would naturally expect to be returned to their shell after executing a program.  This just makes it look like the program is not responding.  Also, the useless variable is unnecessary - add the * modifier to the %i conversion to inhibit assignment.




int main()



This says that main() can be called with an unspecified number of arguments.  Instead, prefer to explicitly declare that it takes no arguments:

int main(void)





printf("You have to guess a number between 1 and 99.\n");



When the output is a fixed string ending in newline, we can use puts() instead of printf().




    int guess;
    scanf("%i", &amp;guess);



If something that's not a number is entered, then scanf() will fail and guess will be uninitialized (that's Undefined Behaviour, which is Not A Good Thing).  We really, really need to check that scanf() successfully converts 1 input, and take some recovery action if not:

int status;
while ((status == scanf("%i", &amp;guess)) != 1) {
    if (status == EOF) {
        fprintf(stderr, "Read error!\n");
        exit 1;
    }
    scanf("%*[^\n]");  // discard input
    printf("? &gt;");
}


Correctly handling input (and, in particular, unexpected input) is difficult in C!




    else if (guess == random_number)



The if is redundant here, as we've eliminated all other possibilities.  So that can be simply else.