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 their program sort-of works. But the program could be better done, and is just not professional grade. 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.
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
Difficulty is rated Easy, Medium, and Hard.
A puzzle rated
These puzzles involve simple expressions. Start Here.
Puzzles E01 - E21
These puzzles involve simple loops.
Puzzles L01 - L10
Puzzles L11 - L20
Puzzles L21 - L30
Puzzles L31 - L40
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
These puzzles involve arrays.
Puzzles D01 - D10
Puzzles D11 - D20
Puzzles D21 - D30
Puzzles D31 - D40
Puzzles D41 - D50
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
These puzzles involve scoping rules.
Puzzles SL01 - SL10 - Block Scope
Puzzles SL11 - SL20 - File Scope
Puzzles SL21 - SL30 - Linkage
Puzzles SL31 - SL40 - Static
These puzzles involve pointers.
Puzzles P01 - P10
Puzzles P11 - P20
Puzzles P21 - P30
This section is about null terminated strings. You might wish to work this section after going through section P on pointers.
Puzzles S01 - S10
Puzzles S11 - S20
These puzzles involve structs.
Puzzles T01 - T10
Puzzles T11 - T20
Puzzles T21 - T30
Main Tutorial Menu
C Puzzles is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.