Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been writing a small C application that operates on files, and I've found that I have been copy+pasting this code around my functions:

char fullpath[PATH_MAX];
fullpath[0] = '\0';
strcat(fullpath, BASE_PATH);
strcat(fullpath, subdir);
strcat(fullpath, "/");
strcat(fullpath, filename);

// do something with fullpath...

Is there a better way? The first thought that comes to mind is to create a macro but I'm sure this is a common problem in C, and I'm wondering how others have solve it.

share|improve this question
sprintf(fullpath, "%s%s/%s", BASE_PATH, subdir, filename)? Besides being less wordy, it'd also resolve your Schlemiel the Painter issue. :P –  cHao Mar 26 at 18:20
@cHao I rarely program in C, but isn't the printf family of functions unsafe (as in, undefined behavior if you screw up)? –  Doval Mar 26 at 18:43
@Doval: Nearly everything in C can cause undefined behavior if you screw up. strcat isn't any safer, really, except for the fixed argument list. –  cHao Mar 26 at 18:48
@cHao Thanks, sprintf looks like the most straightforward solution (also thanks for the "Schlemiel the Painter" reference) –  hyphen this Mar 26 at 19:04
@Doval: Gcc and clang both support type-checking printf and scanf-family functions and any custom functions that forward their arguments to these (with a special annotation). –  Jan Hudec Jun 2 at 14:43

3 Answers 3

Reusable for a function like the one you describe means several things:

  • Doesn't assume the input is valid.
  • Uses sane defaults.
  • Prevents buffer overruns.
  • Returns some indication of success or failure.

In your case:

#define BASE_PATH "/path/to/wherever"
bool build_path(char *dest, size_t size, char *subdir, char *filename)
    // Don't assume the input is valid
    if ( (dest == NULL) || (size < 1) || (filename == NULL) ) {
        return false;

    // Make no subdir work  (sane default behavior)
    if ( subdir == NULL ) {
        subdir = "";

    // Prevent buffer overruns by using a safe formatting function
    size_t stored = snprintf(dest, size, BASE_PATH "/%s/%s", subdir, filename);

    // Returns success (true) if the path fit the buffer
    return ((stored+1) <= size);

char fullpath[PATH_MAX];
if ( ! build_path(fullpath, sizeof(fullpath), "foo/bar", "baz") ) {
   // Handle error
share|improve this answer

How about passing the address of fullpath, subdir, and filename into a function and have that function concatenate all of the strings for you?

Something to the effect of:

void main(...)
    char fullpath[PATH_MAX];
    memset(fullpath, 0x00, PATH_MAX); //note: probably wrong syntax for memset
    ConcatPath(&fullpath, subdir, filename);

ConcatPath(char *fullpath, char *subdir, char *filename)
    strcat(fullpath, BASE_PATH);  //note need some casting magic here to reference
    strcat(fullpath, subdir);     // fullpath & other strings correctly.
    strcat(fullpath, "/");
    strcat(fullpath, filename);
share|improve this answer
(technically, memset(fullpath, 0, PATH_MAX * sizeof(char)), but assuming that a char is 1 byte wide it should work anyway) –  amon Mar 26 at 18:30
@amon sizeof(char) is always 1, no matter how many bits there are in it –  ratchet freak Mar 26 at 18:49
@amon Does C not guarantee that a char is a byte? (I know C++ does...) –  cHao Mar 26 at 18:49
@cHao According to C99, the sizeof operator returns the width in bytes, but sizeof(char) is always 1. We can therefore deduce that a char is always one byte wide (and indeed, C defines a byte as an adressable unit that can hold a char. The confusing part is that a char must be at least 8 bits wide, which means that C's concept of a byte could be different from a hardware byte on exotic architectures). In my previous comment, I was being as vague as possible because I didn't want to accidentally state something that would actually be undefined. –  amon Mar 26 at 20:02
This will break badly if the arguments point to strings whose combined length plus that of BASE_PATH, the slash and the NUL at the end exceeds the size of the array. –  Blrfl Jun 2 at 13:14

In my opinion, as a professional programmer, you must not write code like that. You must either write your own path concatenation function, or use the one provided by the platform.

If it's Windows you should use the _makepath function. For greater safety, use the _makepath_s function. It will be more careful than you are. I'm sure Posix has something similar.

If you must write your own, simple concatenation is just not good enough. You need to check for proper handling of delimiters, trailing slash on directories, leading period on extensions, null arguments and all that stuff. The good news is that if you do it once it's done.

In the more general case of concatenating strings, you are very hampered by your apparent decision to stick with C rather than C++. I would again recommend writing your own function with variable arguments (vararg) and using strcat_s() for security. Something like:

bool strcat_multi_s(char *destination, size_t maxlength, ...); // return false on overflow

I leave the coding as an exercise for the reader.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.