"I want to make this more fun" is the single best driver of your coding skill.
Build the basic version first, then put your own spin on it through the extension challenges.
Practice input, conditions, loops, and random numbers in one game. No graphics library is required.
I have chosen a number from 1 to 100.
This interactive demo runs in JavaScript. The download below is a complete C program you can run on your own computer.
guess-number.c: download this file. gcc -std=c11 -Wall -Wextra guess-number.c -o guess-number ./guess-number
On macOS with command-line tools installed, replace gccwith clang. On Windows with GCC or MinGW, compile using -o guess-number.exe, then run .\guess-number.exe in PowerShell.
/* Number guessing game: C11, no external libraries. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void) {
char input[128];
unsigned int attempts = 0;
srand((unsigned int)time(NULL));
const int answer = rand() % 100 + 1;
puts("Guess a number between 1 and 100!");
for (;;) {
fputs("Your guess> ", stdout);
fflush(stdout);
if (!fgets(input, sizeof input, stdin)) {
puts("\nGame ended.");
return ferror(stdin) ? 1 : 0;
}
if (!strchr(input, '\n') && !feof(stdin)) {
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
puts("Input too long. Enter a number from 1 to 100.");
continue;
}
errno = 0;
char *end;
long guess = strtol(input, &end, 10);
if (end == input) {
puts("Enter an integer from 1 to 100.");
continue;
}
while (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n') ++end;
if (errno == ERANGE || *end != '\0' || guess < 1 || guess > 100) {
puts("Enter an integer from 1 to 100.");
continue;
}
++attempts;
if (guess < answer) puts("Too low! Try a larger number.");
else if (guess > answer) puts("Too high! Try a smaller number.");
else {
printf("Correct! You used %u attempt(s).\n", attempts);
return 0;
}
}
}The code uses fgetsand strtol to reject nonnumeric text, overly long input, and out-of-range values. Program messages are in English. rand is suitable for this exercise, but not for passwords or other security-sensitive uses.
End the game if the player has not guessed correctly after seven attempts. Then try Tic-tac-toe to practice two-dimensional arrays, and Hangman to practice strings.
Review: rand and srand · while loops · if conditions
Updated: 2026-10-01
srand(time(NULL)) once, then rand() % 100 + 1 to get a number from 1 to 100.while until the guess is correct. Read a whole line with fgets, then validate and convert it with strtol.int counter for tries, then print it when the player wins./* Number guessing game: C11, no external libraries. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void) {
char input[128];
unsigned int attempts = 0;
srand((unsigned int)time(NULL));
const int answer = rand() % 100 + 1;
puts("Guess a number between 1 and 100!");
for (;;) {
fputs("Your guess> ", stdout);
fflush(stdout);
if (!fgets(input, sizeof input, stdin)) {
puts("\nGame ended.");
return ferror(stdin) ? 1 : 0;
}
if (!strchr(input, '\n') && !feof(stdin)) {
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
puts("Input too long. Enter a number from 1 to 100.");
continue;
}
errno = 0;
char *end;
long guess = strtol(input, &end, 10);
if (end == input) {
puts("Enter an integer from 1 to 100.");
continue;
}
while (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n') ++end;
if (errno == ERANGE || *end != '\0' || guess < 1 || guess > 100) {
puts("Enter an integer from 1 to 100.");
continue;
}
++attempts;
if (guess < answer) puts("Too low! Try a larger number.");
else if (guess > answer) puts("Too high! Try a smaller number.");
else {
printf("Correct! You used %u attempt(s).\n", attempts);
return 0;
}
}
}
char *hands[] = {"Rock","Scissors","Paper"};(player - cpu + 3) % 3 returns 0 = draw, 1 = lose, 2 = win.-1, then print the record.// 0=Rock, 1=Scissors, 2=Paper int result = (player - cpu + 3) % 3; if (result == 0) printf("Draw\n"); else if (result == 2) printf("You win!\n"); else printf("You lose...\n");
char *symbols[] = {"🍒","🍋","🔔","⭐","7"};rand() % num_symbols three times.char board[3][3]. Empty = ' ', O = 'O', X = 'X'.void print_board(char board[3][3]) that draws separator lines between rows.int check_win(char b[3][3], char c) { for (int i = 0; i < 3; i++) { if (b[i][0]==c && b[i][1]==c && b[i][2]==c) return 1; // row if (b[0][i]==c && b[1][i]==c && b[2][i]==c) return 1; // col } if (b[0][0]==c && b[1][1]==c && b[2][2]==c) return 1; // diag if (b[0][2]==c && b[1][1]==c && b[2][0]==c) return 1; // diag return 0; }
char maze[H][W], where '#' = wall, '.' = path, 'P' = player, 'G' = goal.struct Pos { int x, y; };struct Card { int suit; int rank; };char *words[] = {"programming","computer",...};int revealed[MAX].struct Character { char name[20]; int hp, max_hp, mp, atk, def; };int calc_damage(int atk, int def) — add some random variance.int mines[H][W] for mine locations and int visible[H][W] for what's been revealed.termios.h on Linux/Mac or conio.h on Windows. The easiest starting point is a turn-based version that waits for each keystroke.