Skip to main content Link Menu Expand (external link) Document Search Copy Copied

chdir function

C

Function

Reference manual

@see man chdir

The chdir function is used to change the current working directory of the calling process to the specified directory.

Syntax

#include <unistd.h>

int chdir(const char *path);

Parameters

  • path: A pointer to a null-terminated string containing the path of the directory to change to.

Return value

  • If the directory is successfully changed, the function returns 0.

  • If an error occurs, -1 is returned, and errno is set to indicate the error.

Usage example

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    if (chdir("/path/to/directory") == -1)
    {
        perror("chdir");
        return (EXIT_FAILURE);
    }
    printf("Current working directory changed successfully\n");
    return (EXIT_SUCCESS);
}