How to get the current directory in a C program?

I'm making a C program where I need to get the directory that the program is started from. This program is written for UNIX computers. I've been looking at opendir() and telldir(), but telldir() returns a off_t (long int), so it really doesn't help me.

How can I get the current path in a string (char array)?


  • Have you had a look at getcwd()?

    #include <unistd.h>
    char *getcwd(char *buf, size_t size);

    Simple example:

    #include <unistd.h>
    #include <stdio.h>
    #include <limits.h>
    int main() {
       char cwd[PATH_MAX];
       if (getcwd(cwd, sizeof(cwd)) != NULL) {
           printf("Current working dir: %s\n", cwd);
       } else {
           perror("getcwd() error");
           return 1;
       return 0;