Create Directory or Folder with C/C++ Program

Creating a folder or directory on your computer is an important task in programming. A directory is like a container that helps store and organize files. In C, you often need to create directories to store data, logs, or configuration files.

Creating directories makes file management easier. For example, if your program needs to store logs, it should check if a "logs" folder exists. If not, it can create the folder automatically, so you don't have to do it manually.

In this article, we'll show you how to create a directory in C using the mkdir() function.

Syntax

#include <sys/stat.h>

int mkdir(const char *pathname, mode_t mode);

Parameters

  • pathname − The path and name of the directory to create
  • mode − The permissions for the directory (typically 0755 or 0777)

Return Value

The mkdir() function returns 0 on success and -1 on failure. When it fails, it sets the errno variable to indicate the error.

Example 1: Basic Directory Creation

In this example, we create a simple directory using mkdir()

#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int main() {
    const char *dirName = "logs";
    
    // Create directory with read, write, execute permissions for owner
    // read and execute for group and others
    if (mkdir(dirName, 0755) == 0) {
        printf("Directory '%s' created successfully.\n", dirName);
    } else {
        printf("Failed to create directory '%s': %s\n", dirName, strerror(errno));
    }
    
    return 0;
}
Directory 'logs' created successfully.

Example 2: Checking if Directory Exists Before Creation

This example shows how to check if a directory already exists before attempting to create it −

#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int directoryExists(const char *path) {
    struct stat statbuf;
    if (stat(path, &statbuf) != 0) {
        return 0; // Directory doesn't exist
    }
    return S_ISDIR(statbuf.st_mode); // Return 1 if it's a directory
}

int main() {
    const char *dirName = "data";
    
    if (directoryExists(dirName)) {
        printf("Directory '%s' already exists.\n", dirName);
    } else {
        if (mkdir(dirName, 0755) == 0) {
            printf("Directory '%s' created successfully.\n", dirName);
        } else {
            printf("Failed to create directory '%s': %s\n", dirName, strerror(errno));
        }
    }
    
    return 0;
}
Directory 'data' created successfully.

Key Points

  • The mkdir() function is part of the POSIX standard and works on Unix-like systems
  • Permission mode 0755 gives read/write/execute to owner, read/execute to group and others
  • Always check the return value and handle errors using errno
  • Use stat() function to check if a directory already exists

Comparison Table

Aspect mkdir() Function Manual Creation
Speed Very Fast Slow
Automation Automatic Manual
Error Handling Programmable User-dependent
Portability Cross-platform (with proper headers) Platform-specific

Conclusion

The mkdir() function in C provides a simple and effective way to create directories programmatically. It's essential to check return values and handle errors properly for robust directory creation operations.

Updated on: 2026-03-15T12:46:12+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements