🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of chrisbodhi

chrisbodhi's solution

to Hello World in the C Track

Published at Sep 24 2020 · 0 comments
Instructions
Test suite
Solution

The classical introductory exercise. Just say "Hello, World!".

"Hello, World!" is the traditional first program for beginning programming in a new language or environment.

The objectives are simple:

  • Write a function that returns the string "Hello, World!".
  • Run the test suite and make sure that it succeeds.
  • Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

This is an exercise to introduce users to using Exercism http://en.wikipedia.org/wiki/%22Hello,_world!%22_program

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_hello_world.c

// Include the test framework.
#include "vendor/unity.h"

// Include the header file with the declarations of the functions you create.
#include "../src/hello_world.h"

// Runs before every test.
void setUp(void)
{
}

// Runs after every test.
void tearDown(void)
{
}

// Defines a single test.
static void test_hello(void)
{
   // Check if the 'hello()' function returns "Hello, World!"
   // This test is expected to fail after first downloading this exercise.
   // To make this test pass, fix the 'hello()' function definition in the
   // source file src/hello_world.c.
   TEST_ASSERT_EQUAL_STRING("Hello, World!", hello());
}

// Runs the test(s)
int main(void)
{
   UnityBegin("test/test_hello_world.c");

   RUN_TEST(test_hello);

   return UnityEnd();
}
// Include the standard definitions header from the standard library, so that we
// have access to 'NULL'. This can be removed if your changes remove the need
// for 'NULL'.
#include <stddef.h>

#include "hello_world.h"

// Define the function itself.
const char *hello(void)
{
   // To fix this function, change the return statement here to instead return
   // a string equivalent to the string expected by the failing test.
   return "Hello, World!";
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?