C Puzzles

What this is: These pages contain a collection of C puzzles and solutions. The puzzles are short exercises that stress the common building blocks that go into larger programs. Their purpose is to supplement a C textbook and to provide a way to review what you already know. These pages are not enough, in themselves, to teach the language.

To learn any subject, you need to practice it. To learn C, you need to write dozens of short programs so that the syntax rules and common strategies become second nature.

The language: The language used for these puzzles is ANSI C. The particular programming environment is Bloodshed Dev-C++ . However, any C++ environment should work. All C++ compilers also compile ANSI C.

How to use the puzzles: Start at the beginning of the puzzles and solve them one-by-one in order. The puzzles start out easy and gradually become more difficult. Try to work each puzzle. If you merely read the puzzle and then look at the answer you are not using your time effectively.

Don't rush through these. Do a few puzzles, and then stop. Later in the day, or the next day, when ever you have some time available, do a few more. Short practice sessions are more effective than long ones.

Improve Things: Often students stop work as soon as they get their program sort of works. But their program could be better done, and is just not professional grade. Just as with other subjects, you can learn a great deal by revising and improving your work.

Break Things: If you have a mostly working program, find ways in which it could fail, and then fix the problems. Look for dangerous assumptions that you have made (such as assuming that the data will always be positive, or that a buffer is big enough, or that an array will always contain some data.) Some of the suggested solutions may have such problems. Try to find them.

Console Applications: All of these puzzles are "console applications" which means that standard input is the keyboard and that standard output is the console (on Windows machines, the "DOS Window", sometimes called the "command prompt window"). With Dev-C++ a command prompt window will automatically be created if you compile and run an ordinary source file, one not part of a Windows Application project.

system("pause"): When a console application runs from within a program development environment such as Dev-C++, the system sends standard output to a new console window it creates. But when the program stops running, the window and all the program's output vanishes. To prevent this, put system("pause") at the end of the program. This keeps the console window on the screen until you hit a key. Another way to do this is to put the statement getchar() at the end of your program.

Difficulty Rating: Difficulty is rated Easy, Medium, and Hard. A puzzle rated [E-8] has eight lines of easy code. A puzzle rated [M-10] has ten lines of medium difficulty code. A puzzle rated [H-5] has only five lines of code, but those lines are hard to write. The number of lines of code includes lines in the function body, not counting blank lines, braces, doucumentation, or lines contained in a previous answer.

Part A — Loops

These puzzles involve simple loops.

Puzzles A01 - A10

Puzzles A11 - A20

Puzzles A21 - A30

Puzzles A31 - A40

Part B — Random Numbers

These puzzles involve using random numbers.

Puzzles B01 - B15

Part C — 1D Arrays

These puzzles involve arrays.

Puzzles C01 - C10

Puzzles C11 - C20

Puzzles C21 - C30

Puzzles C31 - C40

Puzzles C41 - C50

Part D — 2D Arrays

These puzzles involve two dimensional arrays. Two dimensional arrays are much less common than one dimensional arrays, but they can be useful.

Puzzles D01 - D10

Puzzles D11 - D20

Part E — Scope and Linkage

These puzzles involve scoping rules.

Puzzles E01 - E10 - Block Scope

Puzzles E11 - E20 - File Scope

Puzzles E21 - E30 - Linkage

Puzzles E31 - E40 - Static

Part F — Pointers

These puzzles involve pointers.

Puzzles F01 - F10

Puzzles F11 - F20

Puzzles F21 - F30

Part G — structs

These puzzles involve structs.

Puzzles G01 - G10

Puzzles G11 - G20

Puzzles G21 - G30

Part S — Strings

This section is about null terminated strings. You might wish to work this section after going through section F on pointers.

Null-terminated Strings

Puzzles S01 - S10

Puzzles S11 - S20

Creative Commons License
C Puzzles by Bradley Kjell is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.