FizzBuzz with debugging output This is a pretty simple FizzBuzz variant. I'm more interested in my use of C (this is my first bit of C code) than I am in my actual FizzBuzz implementation, although I'm always open to suggestions.

In particular I'm pretty certain that my string manipulations are horrific, but I don't really know where to start improving.

I've also added in a little debug utility that I've created, with the purpose of implementing some stuff with header files, as those are completely new to me. All it does is toggle whether a print method will do anything, based on a global flag; I'm not sure if this is actually useful, or if I'm overlooking a better equivalent.



FizzBuzz Source

#include &lt;string.h&gt;
#include "debug_util.c"

void evaluate(char*, int, int, char*);
const int MAX_STRING_LEN = 17;
const int MAX_WORD_LEN = 7;
const int MAX_DIGIT_COUNT_LEN = 4;
const bool LOCAL_DEBUG_FLAG = false;

int main()
{
    DEBUG = LOCAL_DEBUG_FLAG;
    int i;
    char output[MAX_STRING_LEN];
    char intVal[MAX_DIGIT_COUNT_LEN];

    printDebug("Initial Loop Start\n");

    for (i = 1; i &lt;= 100; i++)
    {
        printDebug("Setting initial output to null\n\n");
        /* Set first value in array to null termination,
         * effectively clearing the array,
         * if it is only read as a sequential string */
        output[0] = '\0';

        snprintf(intVal, MAX_DIGIT_COUNT_LEN,"%d",i);

        evaluate(output, 3, i, "Fizz");
        evaluate(output, 4, i, "Buzz");
        evaluate(output, 5, i, "Sizzle");

        output[sizeof(output)] = '\0';

        if (output[0] == '\0')
        {
            strncat(output, intVal, MAX_DIGIT_COUNT_LEN);
        }

        printf("%s\n", output);
    }
}

void evaluate(char *buf, int factor, int value, char *result)
{
    printDebug("Evaluating || Factor: %d, Value: %d, Success Result: %s\n", factor, value, result);
    char temp[MAX_WORD_LEN];

    strncpy(temp, ((value % factor == 0) ? result : ""), MAX_WORD_LEN -1);

    printDebug("Evaluating complete || Result: \"%s\"\n\n", temp);

    //Check array is not empty
    if (value % factor == 0)
    {
        strncat(buf, temp, MAX_STRING_LEN -1);
    }
}




Debug Util Header

#ifndef C_EXAMPLES_DEBUG_UTIL_H
#define C_EXAMPLES_DEBUG_UTIL_H

#ifndef _STDIO_H_
#include &lt;stdio.h&gt;
#endif

#include &lt;zconf.h&gt;

#ifndef _STDBOOL_H
#include &lt;stdbool.h&gt;
#endif

extern bool DEBUG;

#endif //C_EXAMPLES_DEBUG_UTIL_H




Debug Util C Source

#include "debug_util.h"

bool DEBUG = false;

//Only prints if the global DEBUG flag has been set to true
//Replicates code from printf()
int printDebug(const char *format, ...)
{
    int done = 0;
    if (DEBUG)
    {
        va_list arg;

        va_start (arg, format);
        done = vfprintf (stdout, format, arg);
        va_end (arg);
    }

    return done;
}
#includeing source files

A very big issue - especially if you're planning on writing a library:

You include a C source file to use printDebug. You shouldn't do that to avoid possible name collisions between the #includeing file and the #included file, as all of the advantages you had from the header guards are absent when you import the source file itself instead of the header.

The header represents the public interface of the code, so always include that to avoid name collision issues and not depend on implementation details, like internal functions (not that you do that now, but for the future).

The problem with doing this right now would be that you have not put your function declarations in the header. Declare printDebug in the header (I guess you already know how to do this, as you've already done it for evaluate), and you're ready to use it from your main source - just include debug_util.h instead of debug_util.c.

Weird and missing #includes

What is that #include &lt;zconf.h&gt; doing in debug_util.h? as far as I could find out, it's for the compression library zlib, which you don't seem to be using. Also, you should #include &lt;stdarg.h&gt; in debug_util.h to get standards-compliant behavior and compiler portability for the va_* macros you use in debug_util.c.

You should #include &lt;stdio.h&gt; in your main file too, as you're using printf. The header guard is in debug_util.h to save you from the multiple-include problem.

Style and Convention


Your coding style is fine, but you should use int main(void) to indicate that main takes no parameters, as main() indicates (counterintuitively) that main
 can take any number of parameters.
return 0; at the end of main is only optional in C99 mode and above, so you might want to document that.
ALL_CAPS names are generally reserved for macro definitions - use normal variable naming for constants.
DEBUG as declared in debug_util.c and debug_util.h could be const bool - don't assign it in debug_util.c and just leave it for the user to do it. 
Or you could use a DEBUG macro definition and #ifdef/#ifndef checks to do this, as every debug library does by convention; using #ifdef checks means you can even choose to leave out the compilation of printDebug in release builds (it can be an empty function in release builds given its conditional definition, which would cause the call to printDebug to be optimized away.

An example of this idea:

debug_util.h



#ifndef DEBUG_UTIL_H
#define DEBUG_UTIL_H

#ifndef _STDIO_H_
#include &lt;stdio.h&gt;
#endif

#ifndef _STDBOOL_H
#include &lt;stdbool.h&gt;
#endif

int printDebug(const char*, ...);        

#endif //DEBUG_UTIL_H


debug_util.c



#include "debug_util.h"

//Only prints if the global DEBUG flag has been set to true
//Replicates code from printf()
int printDebug(const char *format, ...)
{
#ifdef DEBUG
     int done = 0;
     va_list arg;

     va_start (arg, format);
     done = vfprintf (stdout, format, arg);
     va_end (arg);
#else
     return 1;
#endif
}


In the main code, just do a #define DEBUG 1 after #include "debug_util.h", or pass the -DDEBUG=1 option to gcc when compiling the main code.