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 Dev-C++. However, any C++ environment should work. gcc from the command line works well. All C++ compilers also compile ANSI C.

Here is an on-line C compiler that works well with these puzzles: tutorialspoint.

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 from the keyboard and that standard output is to the monitor (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 integrated development environment such as Dev-C++ the system sends standard output to a new monitor window it creates. But when the program stops running, the window vanishes and you can't see what the program did. 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 E — Expressions

These puzzles involve simple expressions. Start Here.

Puzzles E01 - E21

Part L — Loops

These puzzles involve simple loops.

Puzzles L01 - L10

Puzzles L11 - L20

Puzzles L21 - L30

Puzzles L31 - L40

Part R — Random Numbers

These puzzles involve using random numbers. Random numbers are surprisingly important in programming. Many of the puzzles in the following sections use random numbers.

Puzzles R01 - R15

Part 1D — 1D Arrays

These puzzles involve arrays.

Puzzles D01 - D10

Puzzles D11 - D20

Puzzles D21 - D30

Puzzles D31 - D40

Puzzles D41 - D50

Part 2D — 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 DD01 - DD10

Puzzles DD11 - DD17

Part SL — Scope and Linkage

These puzzles involve scoping rules.

Puzzles SL01 - SL10 - Block Scope

Puzzles SL11 - SL20 - File Scope

Puzzles SL21 - SL30 - Linkage

Puzzles SL31 - SL40 - Static

Part P — Pointers

These puzzles involve pointers.

Puzzles P01 - P10

Puzzles P11 - P20

Part S — Strings

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

Null-terminated Strings

Puzzles S01 - S10

Puzzles S11 - S20

Part T — structs

These puzzles involve structs.

Puzzles T01 - T10

Puzzles T11 - T20

Puzzles T21 - T30