Skip to content

C

Version

Running C on GCC v12.2.0

Supported languages

C

Testing framework

criterion

Special reminders and implementation details

c
#include <criterion/criterion.h>
#include <criterion/redirect.h>
#include <unistd.h>
#include "HelloWorld.h"

TestSuite(HelloWorld);

Test(HelloWorld, should_display_hello_world)
{
    cr_redirect_stdout();

    print(STDOUT_FILENO, "Hello World");
    cr_assert_stdout_eq_str("Hello World");
}

Included libraries

How to debug

Debugging C code is crucial for every developer. One of the simplest yet effective ways to debug is by using printf statements.

Steps to Debug Using printf Statements

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert printf Statements: Add printf statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here’s an example of how to use printf for debugging:

C Code

c
#include <stdio.h>

int calculate_sum(int a, int b) {
    printf("calculate_sum called with arguments: %d, %d\n", a, b); // Log function call
    int sum = a + b;
    printf("Sum after addition: %d\n", sum); // Log sum calculation
    return sum;
}

int main() {
    int result = calculate_sum(5, 10);
    printf("Result of calculate_sum: %d\n", result); // Log the result
    return 0;
}