100 C Programming Practice Problems for Beginners
These 100 problems follow the order a C course teaches things in. You don't need to install a compiler: this site compiles your code with GCC and checks it.
1. Input and output #1–9
With scanf, put & in front of the variable to pass its address (character arrays are the only exception).
The format specifier has to match the type: %d for int, %lld for long long, and for double, %lf in scanf and %f in printf.
Syntax refresher: C scanf(): Read Input · C printf(): Format Specifiers and Output · C scanf String Input: %s and fgets
2. Variables and types #10–21
An int only holds values up to about ±2.1 billion.
If a number could be bigger, use long long.
Overflow gives no error, just a wrong value, so it is hard to track down.
A local variable holds a garbage value until you give it one.
- Marbles passed in turn8P0251
- The shorter one stands in front8P0344
- Four lines of true and false8P0217
- Ticket number from a seat code8P0226
- Sale price, or list price if there is none8P0281
- Swap the cards8P0046
- Big sum8P0015
- Truncate, floor, round8P0044
- A siren that keeps sounding until switched off8P0289
- Letter chain8P0045
- Ride capacity8P0086
- Three hops to the power box contact8P0286
3. Arithmetic #22–34
/ between two integers drops everything after the decimal point (7 / 2 is 3).
If you need a fraction, convert one side to double.
The result of % takes the sign of the number being divided, so -7 % 2 is -1.
- Distance per liter8P0216
- Sum of digits8P0006
- How many combinations a luggage lock has8P0423
- Grid distance8P0017
- Grading addition homework8P0222
- Seconds to H:MM:SS8P0013
- Free-throw success rate sheet8P0314
- Weekday after k days8P0016
- Round half to even8P0048
- This year's savings goal rate8P0407
- Powers that fit8P0050
- Closest to the target8P0218
- Where is the square root8P0049
4. Conditionals #35–45
== compares and = assigns.if (x = 5) compiles, but it is always true.
In a switch, a case without break keeps running into the branches below it.
Syntax refresher: C if Statement: if, else if, else · C switch Statement: case, break, default
- A photo fitted to a square frame8P0254
- Letter grades8P0011
- Leap year8P0019
- Locker extra fee8P0372
- Three-day temperature trend8P0227
- Member discount and delivery fee8P0191
- Kite-flying notice board8P0315
- Middle of three8P0020
- Four-operation calculator8P0021
- Password rule checker8P0098
- Due dates that skip closed days8P0431
5. Loops #46–58
for (int i = 0; i < n; i++) is the basic form.
Mix up < and <= and the loop runs once too many or once too few.
When the body has to run at least once, use do { } while (condition);, and don't forget the semicolon at the end.
Syntax refresher: C for Loop · C while Loop and do-while
- Clap game8P0002
- Star triangle8P0007
- One row of the times table8P0022
- One more throw for yut or mo8P0255
- First multiple8P0023
- Street lamps, with one at the very end8P0371
- A reading marathon that grows every day8P0282
- Fibonacci number8P0010
- Counting temperature records8P0219
- Count the digits8P0052
- Loading a delivery truck in order8P0223
- Count down by steps8P0024
- Running total until zero8P0051
6. Arrays #59–70
An array of size n has indexes 0 to n-1.
C does not stop you from going out of bounds, so writing past the end can break another variable or cause a Segmentation fault.
You cannot copy one array to another with =, so move the elements one by one in a loop.
Syntax refresher: C Arrays: Declare, Initialize, and Loop
- Largest number and its position8P0005
- Numbers of the students who retake8P0374
- Showing one page of a board8P0284
- Sort the numbers8P0036
- Sorting sign letters8P0253
- Is it on the list?8P0059
- Swapping places in pairs8P0312
- Travel time on a one-way bus route8P0224
- Remove every match8P0058
- Rotate a list right8P0032
- Row and column sums8P0033
- Number order and dictionary order8P0320
7. Strings #71–82
A C string is a char array with '\0' at the end.
To read 100 characters you need char s[101].
Compare contents with strcmp, not ==, and get the length with strlen.
Syntax refresher: C Strings: char Arrays, strlen, strcpy, strcmp · C scanf String Input: %s and fgets
- Count the vowels8P0009
- First and last position of a character8P0053
- Letters picked along the diagonal8P0283
- Count the words8P0012
- Palindrome check8P0004
- Reverse it, keep the sign8P0252
- Swap the case8P0025
- A price tag with dots and commas swapped8P0412
- Characters left in a message with Korean text8P0341
- Letter numbers in a note8P0192
- Cut from the a-th to the b-th character8P0054
- Reverse the word order8P0028
8. Sum, average, maximum #83–87
Always start a sum variable at 0.
If there are many numbers or they are large, keep the sum in a long long.
To get the average as a fraction, convert before you divide, as in (double)sum / n.
9. Functions #88–95
Define a function before you use it, or write its prototype near the top.
A function that returns a value has to return on every branch.
Arguments are passed as copies, so changing one inside the function leaves the outside variable as it was.
- A thermometer that cannot leave its scale8P0402
- Balls that landed in the sandbox8P0285
- Prime check8P0008
- A running course made of three flags8P0435
- A box-count function with defaults8P0257
- Tidy locker numbers8P0196
- Hall chairs numbered in a zigzag8P0428
- Pick the top word with a key function passed in8P0318
10. A first look at pointers #96–100
&x is the address of x, and *p is the value at the place p points to.
To let a function change an outside variable, pass the address and write to *p.
Swapping two values is the best first exercise.
Syntax refresher: C Pointers Explained for Beginners
Good to know
- Read the warnings too. This site compiles with
-Wall, so a wrong format specifier or a missingreturnshows up as a warning. - Your output has to match down to the last character. Match the
\nand the spaces in yourprintfexactly to the example. - If you get a Segmentation fault, check array bounds and the
&inscanffirst.
See also
- C if-else Practice Problems — 42 problems
- C Loop Practice Problems: for, while, do-while — 50 problems
- C Array and String Practice Problems — 81 problems
- C Pointer Practice Problems — 16 problems
- Learn C — lessons with explanations and examples
- C reference
- C error messages
- Other problem sets