The chdir command is a system function (system call) which is used to change the current working directory. On some systems, this command is used as an alias for the shell command cd. chdir changes the current working directory of the calling process to the directory specified in path.
int chdir(const char *path);
Parameter: Here, the path is the Directory path which the user want to make the current working directory.
Return Value: This command returns zero (0) on success. -1 is returned on an error and errno is set appropriately.
Note: It is declared in
Note:The above program changes the working directory of a process. But, it doesn’t change the working directory of current shell. Because when the program is executed in the shell, the shell follows fork on exec mechanism. So, it doesn’t affect the current shell.
Errors: There can be errors which can be returned. These depend on the filesystem.
- EACCES: If the search permission is denied for one of the components of path.
- EFAULT: If the path points lie outside the accessible address space.
- EIO: If there is an I/O error occurred.
- ELOOP: If there are too many symbolic links were encountered in the resolving path.
- ENAMETOOLONG: If the path is too long.
- ENOENT: If the file does not exist.
- ENOMEM: If there is insufficient kernel memory is available.
- ENOTDIR: If the component of path is not a directory.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference Between Machine Language and Assembly Language
- Difference Between C Language and LISP Language
- C Programming Language Standard
- A C Programming Language Puzzle
- C Language Introduction
- Arrays in C Language | Set 2 (Properties)
- Convert C/C++ code to assembly language
- Signals in C language
- Constants vs Variables in C language
- Difference between %d and %i format specifier in C language
- Boxplots in R Language
- lvalue and rvalue in C language
- Difference between while(1) and while(0) in C language
- kbhit in C language
- How to use POSIX semaphores in C language
- Benefits of C language over other programming languages
- fgets() and gets() in C language
- isxdigit() function in C Language
- isupper() function in C Language
- isalnum() function in C Language
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.