Simple text based game in C After sticking with the same handful of languages for years, I decided to learn C.

I've written a guess the number game, where it generates a random number based on the difficulty level, and then you have 5 attempts of guessing the number. 

If you guess incorrect, it will give you a clue by telling you if the answer is higher or lower than your last guess. I find the game to be pretty interactive, and wanted to get a code review to see how I could improve on it.

This is my first ever C  program and I'm just looking to improve.

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

int generate_random_number(int min, int max) {
    srand ( time(NULL) );
    return min + (rand() % (max - min));
}

void play() {

    int difficulty = 1; // 1 = easy, 2 = medium, 3 = hard, 4 = insane

    printf("1 = EASY, 2 = MEDIUM, 3 = HARD, &amp; 4 = INSANE\n");
    printf("What level would you like to play: ");

    scanf("%i", &amp;difficulty);

    int min = 0;
    int max = 0;
    int random = 0;

    if (difficulty == 1) {
        min = 0;
        max = 25;

        printf("You have selected to play easy\n\n");
    }
    else if (difficulty == 2) {
        min = 0;
        max = 50;

        printf("You have selected to play medium\n\n");
    }
    else if (difficulty == 3) {
        min = 0;
        max = 75;

        printf("You have selected to play hard\n\n");
    }
    else if (difficulty == 4) {
        min = 0;
        max = 100;

        printf("You have selected to play insane\n\n");
    }

    random = generate_random_number(min, max);

    int tries = 5;
    int won = 0;

    while (tries &gt; 0)
    {
        int guess = 1000000; // just so it doesn't accidentally equal to random

        printf("Guess a number %i to %i: ", min, max);
        scanf("%i", &amp;guess);

        if (guess == random) {
            won = 1;
            break;
        }
        else {
            if (guess &gt; random) {
                printf("Incorrect guess, the answer is lower than your guess!\n\n");
            }
            else {
                printf("Incorrect guess, the answer is higher than your guess!\n\n");
            }
        }

        tries -= 1;
    }

    if (won) {
        printf("Congratulations, you have won the game!");
    }
    else {
        printf("Sorry, you are out of tries.\n\n");
    }
}

int main() {
    while (1) {
        play();
    }

    return 0;
}
Good:

no warnings with -Wall -Wextra -pedantic with both gcc and clang, no
memory leaks found with valgrind.

Bad:

use prototypes instead of declarations to give compiler a chance
to issue warnings when an incorrect number of parameters is passed or incorrect types are passed:

void play(void)
int main(void)


You don't check if user passes correct acceptable difficulty level:

$ ./main
1 = EASY, 2 = MEDIUM, 3 = HARD, &amp; 4 = INSANE
What level would you like to play: 6
Floating point exception
$ ./main
1 = EASY, 2 = MEDIUM, 3 = HARD, &amp; 4 = INSANE
What level would you like to play: 0
Floating point exception


You don't check if user passes an integer in the first place:

$ ./main
1 = EASY, 2 = MEDIUM, 3 = HARD, &amp; 4 = INSANE
What level would you like to play: a


will make your program go into an endless loop.

Suggested:

convert int difficulty = 1 into an en enum with additional DIFFICULTY_MAX and DIFFICULTY_MIN values, and check if value passed from the user is lower than DIFFICULTY_MAX and larger or equal to DIFFICULTY_MIN.

Use EXIT_SUCCESS to denote success at the end of main():

return EXIT_SUCCESS;


or just omit the return from main() - compiler will automatically return a success value if we run off the end.