C Tutorial

C Project: Guess the Number

Guess a secret integer. The program prints high, low, or correct and counts the attempts.

What you will build

A guessing game holds one secret integer. Each guess is compared with that secret. The program printslow, high, or correct, and it counts how many guesses it took.

Live games often call rand() so the secret changes. That makes the printed output change every run, which is a poor fit for a tutorial. This page uses a fixed secret and a fixed array of guesses so gcc prints the same lines every time. After the logic is clear, you can swap the array for scanf or a random secret.

Compile the examples with Try it in C at /c/try.

One guess, three outcomes

Start with a single comparison. Three branches cover every integer: strictly less, strictly greater, or equal. Equality is the win.

Example

#include <stdio.h>

int main(void) {
  int secret = 7;
  int guess = 10;

  if (guess < secret) {
    printf("%d is low\n", guess);
  } else if (guess > secret) {
    printf("%d is high\n", guess);
  } else {
    printf("%d is correct\n", guess);
  }
  return 0;
}

Set guess to 3, then to 7, and compile twice more. You should see low, then correct. The secret stays 7 so the result is predictable.

A list of guesses

A game is a loop. Put several attempts in an array. Walk the array from the first element. After each comparison, add one to an attempt counter. Stop when the guess matches the secret, even if unused numbers remain in the array.

Example

#include <stdio.h>

int main(void) {
  int secret = 7;
  int guesses[] = {3, 10, 5, 7, 9};
  int n = (int)(sizeof guesses / sizeof guesses[0]);
  int attempts = 0;
  int won = 0;

  for (int i = 0; i < n; i++) {
    int guess = guesses[i];
    attempts++;
    if (guess < secret) {
      printf("%d is low (attempt %d)\n", guess, attempts);
    } else if (guess > secret) {
      printf("%d is high (attempt %d)\n", guess, attempts);
    } else {
      printf("%d is correct in %d attempts\n", guess, attempts);
      won = 1;
      break;
    }
  }

  if (!won) {
    printf("out of guesses; the secret was %d\n", secret);
  }
  return 0;
}

Run this at /c/try. The fourth value matches, so attempt 4 is the win and9 is never tested. Change the secret to 9 and compile again to see a later win.

When the list never hits

A finished game must handle failure. If the loop ends without a match, print the secret so the player can check the array. This second full program uses a short list that stays too low.

Example

#include <stdio.h>

void report(int guess, int secret, int attempts) {
  if (guess < secret) {
    printf("%d is low (attempt %d)\n", guess, attempts);
  } else if (guess > secret) {
    printf("%d is high (attempt %d)\n", guess, attempts);
  } else {
    printf("%d is correct in %d attempts\n", guess, attempts);
  }
}

int main(void) {
  int secret = 42;
  int guesses[] = {10, 20, 30};
  int n = (int)(sizeof guesses / sizeof guesses[0]);
  int attempts = 0;
  int found = 0;

  for (int i = 0; i < n; i++) {
    attempts++;
    report(guesses[i], secret, attempts);
    if (guesses[i] == secret) {
      found = 1;
      break;
    }
  }

  if (!found) {
    printf("no match after %d attempts; secret was %d\n", attempts, secret);
  }
  return 0;
}

report only prints. main owns the counter and the found flag. Keeping those duties separate makes it easier to change the message later without touching the loop.

Why this demo skips rand

rand() and srand live in <stdlib.h>. A typical range issecret = rand() % 100 + 1 for 1 through 100. Without a fixed seed the number changes, so a printed transcript on this page would not match your next compile. Keep the array until you want a different secret on purpose.

Typed input is the other upgrade. A while loop around scanf("%d", &guess) turns the array into a live player. Do that only after the high / low / correct branches and the attempt count are already correct on hardcoded data.

Common mistakes

  • Using = instead of == in the win test. That assigns the secret to the guess and then treats the value as true.
  • Counting attempts outside the loop, or counting even when you break before a guess is read. Increment once per actual guess.
  • Calling rand() in the graded demo and then wondering why the output does not match these notes. Fix the secret first.
  • Writing if (guess < secret) and if (guess > secret) as two separate ifs without else, then also printing correct. One guess must take exactly one branch.

Practice

  1. Cap the game at five attempts. If the fifth guess is still wrong, print a lose line and the secret.
  2. Track the closest guess (smallest absolute difference) and print it after a loss.
  3. Replace the array with a short while loop and scanf after the hardcoded version is correct.

FAQ: C Project: Guess the Number

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c guess number project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c guess number project in this C C lesson (C Project: Guess the Number).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.