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 quickly review and reinforce 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. Any ANSI C environment will work. Notepad++ and gcc from the command line work well. All C++ compilers compile ANSI C.

On-line C environments can be used. Here is one that works well with these puzzles: tutorialspoint.

How to use the puzzles: Start at the beginning 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 as soon as their program sort-of works. But their program could be improved, and is just not professional grade. As with other subjects, you can learn more by revising and improving your work.

Break Things: If you have a 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 integers are always positive, or that a buffer is big enough, or that an array will always contain 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").

system("pause"): When a console application runs from within some integrated development environments 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 getchar() at the end of the 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

Puzzles SL41 - SL50 - More Static

Part P — Pointers

These puzzles involve pointers.

Puzzles P01 - P10

Puzzles P11 - P20

Puzzles P21 - P30

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 C — Command Line Parameters

How programs can accept parameters entered as part of the command line.

Command Line Parameters

Part T — structs

These puzzles involve structs.

Puzzles T01 - T10

Puzzles T11 - T20

Puzzles T21 - T30

Click here Main Tutorial Menu

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