🎓 Free Capstone Projects with Full Documentation, ER Diagrams & Source Code — Updated Weekly for 2026
👨‍💻 Free Source Code & Capstone Projects for Developers

Hangman Game In C With Source Code

The hangman game code in c is developed using C programming language. This hangman game in c programming is about guessing letters (A-Z) to form the words.

A hangman game is a common word guessing game in which the player must guess one letter at a time to complete a missing word.

This hangman game source code in c language ends after a certain number of incorrect guesses, and the player loses.

If the player correctly recognizes all of the letters in the missing title, the game is also over. In this project design is straightforward and clean, making it easy for users to learn, use, and navigate.

This console game application also includes a downloadable hangman game source code in c language for free, just find the downloadable source code below and click to start downloading.

Anyway if you want level up your knowledge in programming especially C/C++ Programming Language, try this new article I’ve made for you Best C Projects with Source Code for Beginners Free Download 2022.

To run this hangman game program in c language make sure that you have a Code Blocks or any platform of C installed in your computer.

ABOUT PROJECTPROJECT DETAILS
Project Name :hangman game
Project Platform :C/C++
Programming Language Used:C Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Dev-C++/Codeblocks
Project Type :Desktop Application
Database:Stores data in .DAT file
Hangman Game In C With Source Code Information

Hangman Game In C Source Code Steps On How To Run The Project

Time needed: 5 minutes

These are the steps on how to run Hangman Game project In C With Source Code

  • Step 1: Download

    First, download the source code given below.
    Hangman Game In C Step 1

  • Step 2: Extract file.

    Second, after you finished download the source code, extract the zip file.
    Hangman Game In C Step 2

  • Step 3: Open CodeBlocks

    Third, open “CodeBlocks IDE”.

  • Step 4: Open Project.

    Fourth, open file tab and Open File after that open folder HangmanGameC then click the “hangman.c“.

  • Step 5: Run Project

    Fifth, run the project.

  • Step 6: The actual code.

    You are free to copy the given source code below or download the downloadable source code given.

The Code Given Below is the Actual Code of System

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>

#define WORDS 10
#define WORDLEN 40
#define CHANCE 6

bool srand_called = false;

int i_rnd(int i) {
    if (!srand_called) {
        srand(time(NULL) << 10);
        srand_called = true;
    }
    return rand() % i;
}

char* decrypt(char* code) {
	int hash = ((strlen(code) - 3) / 3) + 2;
	char* decrypt = malloc(hash);
	char* toFree = decrypt;
	char* word = code;
	for (int ch = *code; ch != '\0'; ch = *(++code))
	{
		if((code - word + 2) % 3  == 1){
			*(decrypt++) = ch - (word - code + 1) - hash;
		}
	}
	*decrypt = '\0';
	return toFree;
}

void printBody(int mistakes, char* body) {
	printf("\tMistakes :%d\n", mistakes);
	switch(mistakes) {

		case 6: body[6] = '\\'; break;
		case 5: body[5] = '/'; break;
		case 4: body[4] = '\\'; break;
		case 3: body[3] = '|'; break;
		case 2: body[2] = '/'; break;
		case 1: body[1] = ')', body[0] = '('; break;
		default: break;

	}

	printf("\t _________\n"
	       "\t|         |\n"
	       "\t|        %c %c\n"
	       "\t|        %c%c%c\n"
	       "\t|        %c %c\n"
	       "\t|             \n"
	       "\t|             ", body[0], body[1], body[2],
	       body[3], body[4], body[5], body[6]);
}

void printWord(char* guess, int len) {
	printf("\t");
	for (int i = 0; i < len; ++i)
	{
		printf("%c ", guess[i]);
	}
	printf("\n\n");
}

int main() {

	printf("\n\t Be aware you can be hanged!!.");

	printf("\n\n\t Rules : ");
	printf("\n\t - Maximum 6 mistakes are allowed.");
	printf("\n\t - All alphabet are in lower case.");
	printf("\n\t - All words are name of very popular Websites. eg. Google");
	printf("\n\t - If you enjoy continue, otherwise close it.");

	printf("\n\t Syntax : Alphabet");
	printf("\n\t Example : a \n\n");

	char values[WORDS][WORDLEN] = {"N~mqOlJ^tZletXodeYgs","gCnDIfFQe^CdP^^B{hZpeLA^hv","7urtrtwQv{dt`>^}FaR]i]XUug^GI",
									"aSwfXsxOsWAlXScVQmjAWJG","cruD=idduvUdr=gmcauCmg]","BQt`zncypFVjvIaTl]u=_?Aa}F",
									"iLvkKdT`yu~mWj[^gcO|","jSiLyzJ=vPmnv^`N]^>ViAC^z_","xo|RqqhO|nNstjmzfiuoiFfhwtdh~",
									"OHkttvxdp|[nnW]Drgaomdq"};
	char *body = malloc(CHANCE+1);

	int id = i_rnd(WORDS);
	char *word = decrypt(values[id]);
	int len = strlen(word);
	char *guessed = malloc(len);
	char falseWord[CHANCE];

	memset(body, ' ', CHANCE+1);
	memset(guessed, '_', len);
	char guess;
	bool found;
	char* win;

	int mistakes = 0;
	setvbuf(stdin, NULL, _IONBF, 0);

	do {

		found = false;
		printf("\n\n");
		printBody(mistakes, body);
		printf("\n\n");
		printf("\tFalse Letters : ");
		if(mistakes == 0) printf("None\n");
		for (int i = 0; i < mistakes; ++i)
		{
			printf("%c", falseWord[i]);
		}
		printf("\n\n");
		printWord(guessed, len);
		printf("\tGive me a alphabet in lower case : ");
		do {scanf("%c",&guess);} while ( getchar() != '\n' );
		for (int i = 0; i < len; ++i)
		{
			if(word[i] == guess) {
				found = true;
				guessed[i] = guess;
			}
		}
		if(!found) {
			falseWord[mistakes] = guess;
			mistakes += 1;
		}
		win = strchr(guessed, '_');
	}while(mistakes < CHANCE && win != NULL);

	if(win == NULL) {
		printf("\n");
		printWord(guessed, len);
		printf("\n\tCongrats! You have won : %s\n\n", word);
	} else {
		printf("\n");
		printBody(mistakes, body);
		printf("\n\n\tBetter try next time. Word was %s\n\n", word);
	}

	free(body);
	free(word);
	free(guessed);
	return EXIT_SUCCESS;
}

Downloadable Source Code

Code Walkthrough — How the C Version Works

The C version uses fixed-size character arrays instead of the C++ std::string, manual memory management with char[], and the printf/scanf family for I/O. Here is what each section of the typical implementation does:

Word Selection

const char *words[] = {
    "computer", "programming", "hangman",
    "capstone", "developer", "itsourcecode"
};
int word_count = sizeof(words) / sizeof(words[0]);

srand((unsigned int)time(NULL));
const char *secret = words[rand() % word_count];

The srand((unsigned int)time(NULL)) seeds the random generator with the current Unix timestamp so the word changes each run. The sizeof(words) / sizeof(words[0]) trick gets the array length at compile time.

Masked Display

char display[64];
int len = strlen(secret);
for (int i = 0; i < len; i++) display[i] = '_';
display[len] = '\0';

This builds the underscores string that hides the word from the player. The terminating \0 is critical — without it, the array might print garbage beyond the last underscore.

The Guess Loop

int lives = 6;
char guessed[27] = "";

while (lives > 0 && strcmp(display, secret) != 0) {
    printf("Word: %s   Guessed: %s   Lives: %d\n", display, guessed, lives);
    printf("Enter letter: ");
    char g;
    scanf(" %c", &g);
    g = tolower(g);

    if (strchr(guessed, g)) { printf("Already guessed.\n"); continue; }
    int n = strlen(guessed);
    guessed[n] = g; guessed[n+1] = '\0';

    int found = 0;
    for (int i = 0; i < len; i++) {
        if (secret[i] == g) { display[i] = g; found = 1; }
    }
    if (!found) lives--;
}

Three things make this version solid. The space before %c in scanf(" %c", ...) skips whitespace including any leftover newline. The strchr(guessed, g) checks if the letter was already guessed. The strcmp(display, secret) != 0 is the win condition — the loop exits when display equals secret.

Common Errors When Running Hangman in C

  • 'srand' was not declared — missing #include <stdlib.h>.
  • 'tolower' was not declared — missing #include <ctype.h>.
  • 'strchr' / 'strcmp' was not declared — missing #include <string.h>.
  • Same word every run — you forgot srand(time(NULL)). Without it, rand() uses the default seed of 1.
  • Program reads garbage character — you used scanf("%c", ...) without the leading space. The previous newline is consumed instead of the new letter.
  • Display string prints junk — missing the null terminator '\0' after the underscores.
  • Buffer overflow on long words — if your word bank has words longer than 63 chars, display[64] overflows. Use a larger array or compute the size at runtime.

Suggested Enhancements for Your Capstone

  • Difficulty levels — filter the word bank by length: easy = 4-5 letters, medium = 6-7, hard = 8+.
  • ASCII hangman figure — print one of 7 ASCII art stages based on remaining lives. Adds visual feedback.
  • Score tracking — write best time and win count to scores.txt using fopen / fprintf.
  • Word bank from file — read words from words.txt instead of hardcoding. Lets non-programmers expand the game.
  • Categories — sports words, programming terms, BSIT capstone topics. Each in a separate file.

Conclusion

This hangman in c language is only a project made for school requirement purposes only. You can download this source code and modify it to suit your client requirements, since this is a student project it means you cannot really expect 100% functionality from this.

Inquiries

If you have any questions or suggestions about hangman in c description, please feel free to leave a comment below.

Frequently Asked Questions

What is the Hangman game in C?

The Hangman game in C is a classic word-guessing console program written in the C programming language. The computer picks a secret word, displays underscores for each letter, and the player guesses one letter at a time. Each wrong guess reduces a life counter. The player wins by revealing all letters before running out of lives. It is a popular beginner project for learning loops, arrays, and string manipulation in C.

What headers do I need to compile Hangman in C?

Four standard headers: <stdio.h> for printf/scanf, <stdlib.h> for rand and srand, <string.h> for strcmp/strchr/strlen, and <ctype.h> for tolower. Plus <time.h> for the time() function used to seed the random generator.

How do I pick a random word from an array in C?

First seed the random number generator once at the start of main: srand((unsigned int)time(NULL)). Then pick a random index with rand() % array_count. Without the srand call, rand() returns the same sequence every run. The casting (unsigned int) avoids compiler warnings about sign conversion.

What is the difference between Hangman in C and Hangman in C++?

The C version uses fixed-size char[] arrays, manual memory tracking, and printf/scanf for I/O. The C++ version uses std::string (no manual length tracking), std::vector for the word bank, and cin/cout for cleaner I/O. The C version compiles smaller and runs slightly faster but the C++ version is easier to read and modify. Both are good beginner projects.

Why does scanf(“%c”) not work properly in my Hangman loop?

Because scanf("%c") reads the FIRST character in the input buffer, including leftover newlines from the previous input. The fix is to add a space before %c: scanf(" %c", &c). The space tells scanf to skip whitespace (newlines, tabs, spaces) before reading the actual letter. This single character fix solves 90% of C input problems.

Related C and C++ Projects

2 thoughts on “Hangman Game In C With Source Code”

Leave a Comment