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

vlzware's solution

to Space Age in the C Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

Exercises without stub implementations

Like the majority of C programs you will write, this exercise comes without any header file (*.h) or source file (*.c).

Add the required files

You will need to create these yourself as part of the exercise. This is so that you get to practice deciding on and creating the program's interface yourself, which is an important part of programming in C. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

make: *** No rule to make target 'src/exercise_name.c', needed by 'tests.out'.  Stop.

This error occurs because the makefile attempts to use a file that hasn't been created yet (exercise_name.c). To resolve this error you will need to add a matching file to the src directory. For example, for the error above you would add a file called exercise_name.c.

When you try to run the tests again you will get a slightly different error.

make: *** No rule to make target 'src/exercise_name.h', needed by 'tests.out'.  Stop.

Again the makefile still needs another file that has yet to be created (exercise_name.h). The solution to this error is similar to the last, add the matching file to the src directory. For example, for the above error you would add a file called exercise_name.h

Add the required declarations

Running the tests a third time you see you will get new errors, similar to:

Compiling tests.out
src/exercise_name.c:1:0: error: ISO C forbids an empty translation unit [-Werror=pedantic]
cc1: all warnings being treated as errors
test/test_exercise_name.c:13:1: error: unknown type name ‘bar_t’; did you mean ‘__bar_t’?
    bar_t b = foo(a, 2.5);
test/test_exercise_name.c:15:14: error: storage size of fizz isn’t known
    struct baz fizz;
test/test_exercise_name.c:29:20: error: implicit declaration of function ‘foo’ [-Werror=implicit-function-declaration]
    bar_t b = foo_function(fizz, 2.5);

This error means that you need to add function, type and variable decarations to the header file to match those the test file is attempting to use. In doing this you will need to look at the build errors and the test code to determine what needs to be declared in the header file.

For example, for the error above you should declare a type named bar_t, a struct called baz and a function named foo(). Additionally we can tell from looking at the above errors that function foo() has two parameters.

The first parameter is passed an argument of fizz and thus should have a type of struct baz. The second parameter is passed 2.5 and so could have one of two types, either float or double. You will need to look at the test code to determine which.

The names of the parameters are not determined by the test code and so are left up to you to decide, though in keeping with C programming practices they should be descriptive but not overly long.

Further, we can see that the the return type expected by the test code is of the type bar_t.

Putting this all together we end up with a function declaration that looks like the following:

// The parameter names are not very good here they should be more descriptive in a real exercise.
// We have decide on a double for the second parameter in this hypothetical example.
bar_t foo(struct baz b, double d);

You should continue to do this for any further similar errors. To check that you have correctly determined the required declaration, just run the tests again and analyse any errors similarly.

Additionally, remember to add any includes to the header file any headers that it itself requires.

Include guards

Before you are finished with the header file, you should add include guards. Include guards are used to help prevent the file from being included multiple times by accident. If the file was included multiple times then the functions and other items would be declared twice; In C this is an error because the compiler thinks you are trying to declare another different function that has the same name.

To add include guards, add something similar to the following as the first two lines of the header:


And on the very last line:


Add the required definitions

Once the header file is complete you may still have build errors similar to the following:

Compiling tests.out
src/exercise_name.c:1:0: error: ISO C forbids an empty translation unit [-Werror=pedantic]
cc1: all warnings being treated as errors
makefile:24: recipe for target 'tests.out' failed
make: *** [tests.out] Error 1

This is because although you have declared all the items you need to, they have not yet been defined. To define the needed items you need to add their implementation to the exercise_name.c file. For the function foo() from the previous example, this would look similar to:

#include "exercise_name.h"

bar_t foo(struct baz b, double d)
    // Your exercise code here

After having resolved these errors you should be ready to start making the tests pass!

Getting Started

Make sure you have read the C page 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.


Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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


#include "vendor/unity.h"
#include "../src/space_age.h"

void setUp(void)

void tearDown(void)

void test_convert_earth_year(void)
   TEST_ASSERT_FLOAT_WITHIN(1, 31.69, convert_planet_age(EARTH, 1000000000));

void test_convert_mercury_year(void)
   TEST_IGNORE();               // delete this line to run test
   TEST_ASSERT_FLOAT_WITHIN(3, 280.88, convert_planet_age(MERCURY, 2134835688));

void test_convert_venus_year(void)
   TEST_ASSERT_FLOAT_WITHIN(1, 9.78, convert_planet_age(VENUS, 189839836));

void test_convert_mars_year(void)
   TEST_ASSERT_FLOAT_WITHIN(1, 39.25, convert_planet_age(MARS, 2329871239));

void test_convert_jupiter_year(void)
   TEST_ASSERT_FLOAT_WITHIN(0.1, 2.41, convert_planet_age(JUPITER, 901876382));

void test_convert_saturn_year(void)
   TEST_ASSERT_FLOAT_WITHIN(0.1, 3.23, convert_planet_age(SATURN, 3000000000));

void test_convert_uranus_year(void)
   TEST_ASSERT_FLOAT_WITHIN(0.1, 1.21, convert_planet_age(URANUS, 3210123456));

void test_convert_neptune_year(void)
   TEST_ASSERT_FLOAT_WITHIN(0.1, 1.58, convert_planet_age(NEPTUNE, 8210123456));

int main(void)


   return 0;


#include "space_age.h"

const long int EARTH_YEAR = 31557600;

const float orb_period[COUNT_PLANETS] = {

float convert_planet_age(enum planets planet, long int secs)
	return (float) (secs / (orb_period[planet] * EARTH_YEAR));


#ifndef SPACEAGE_H
#define SPACEAGE_H

enum planets {
		EARTH = 0,

float convert_planet_age(enum planets, long int secs);


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?