A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
Windows does not have POSIX permission bits. There is no mode argument. The _mkdir function in direct.h takes a single path string and creates the directory. It returns 0 on success and -1 on failure, setting errno to EEXIST if the directory already exists. The underscore prefix is Microsoft's convention for functions that are not part of the ISO C standard.
#include <stdio.h>#include <direct.h>#include <errno.h>#include <string.h>int main(void) {const char *dir = "output_data";int result = _mkdir(dir);if (result == 0) {printf("Created: %s\n", dir);} else if (errno == EEXIST) {printf("Already exists: %s\n", dir);} else {printf("Failed: %s\n", strerror(errno));}return 0;}
If your code must compile on both platforms, wrap the call in a preprocessor conditional. This is the standard approach in portable C libraries.
#include <stdio.h>#ifdef _WIN32#include <direct.h>#define make_dir(path) _mkdir(path)#else#include <sys/stat.h>#define make_dir(path) mkdir(path, 0755)#endifint main(void) {if (make_dir("cross_platform_dir") == 0) {printf("Directory created.\n");} else {perror("make_dir");}return 0;}
The _WIN32 macro is defined by both 32-bit and 64-bit Windows compilers, including MSVC, MinGW, and Clang targeting Windows. It is the most reliable platform detection macro. On POSIX systems, the mkdir call includes the mode argument. On Windows, it is omitted. The make_dir macro hides the difference from the rest of your code.
Platform-specific directory functions are a small but recurring annoyance in portable C. The wrapper pattern shown here scales to file operations, path separators, and environment variable access. Next: _getcwd, for retrieving the current working directory on Windows.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.