Skip to content
  • There are no suggestions because the search field is empty.

Create a C Coding Challenge

Build and test a C coding challenge in LeetLabs, with a complete example.

This guide shows how to build a C coding challenge in LeetLabs, with a complete example that we built and tested on Learnyst. Use it as a template for your own C exercises. Read Create a coding challenge first for the general steps.

On this page

How input and output work in C

Your program reads the test case Input from standard input with scanf and prints its answer with printf. Include stdio.h and return 0 from main.

The program's printed output must match the test case Output exactly, including spaces and line breaks.

Example: Sum of two numbers

Create a challenge with Language set to C, then fill in each step.

Problem Statement:

Sum of Two Numbers
Read two integers a and b from a single line of input, separated by a space. Print their sum.
Example input: 2 3
Example output: 5
Constraints: -1000 <= a, b <= 1000

Initial Snippet (the starter code learners see):

#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    // Write your code below
    return 0;
}

Test Cases:

Title Input Output Visibility
Positive numbers 2 3 5 Visible
Negative numbers -4 10 6 Hidden

Hints: one to explain the input is already read, and one to show how to print the result.

Solution:

#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d\n", a + b);
    return 0;
}

Advanced Options: Difficulty Easy, Unlock Hints After 1 code run.

Test it in Preview

Click Preview, then TAKE CHALLENGE. Learners see the problem on the left, your starter code in the editor, and the visible test case below.

C challenge in the learner coding screen

Code that subtracts instead of adding prints -1 for the input 2 3, so RUN shows Wrong Answer.

Wrong Answer result

The correct solution prints 5 and shows Correct Answer. The hidden test case is checked when the learner submits.

Correct Answer result

Tips for C challenges

  • Match the scanf format to the input, for example "%d %d" for two integers on one line.
  • End the output with \n as shown in the solution.
  • Use long long and %lld when values can be very large.

Practice ideas for C

  • Swap two numbers using pointers
  • Find the factorial of n
  • Reverse an array
  • Count digits in a number
  • Print a star pattern

What to do next

If you have questions, contact us at support@learnyst.com.