ในการรับไดเรกทอรีปัจจุบัน (ที่คุณรันโปรแกรมเป้าหมายของคุณ) คุณสามารถใช้โค้ดตัวอย่างต่อไปนี้ซึ่งใช้ได้กับทั้ง Visual Studio และ Linux / MacOS (gcc / clang) ทั้ง C และ C ++:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER)
#include <direct.h>
#define getcwd _getcwd
#elif defined(__GNUC__)
#include <unistd.h>
#endif
int main() {
    char* buffer;
    if( (buffer=getcwd(NULL, 0)) == NULL) {
        perror("failed to get current directory\n");
    } else {
        printf("%s \nLength: %zu\n", buffer, strlen(buffer));
        free(buffer);
    }
    return 0;
}