rename() function is used to change the name of the file or directory i.e. from old_name to new_name without changing the content present in the file. This function takes name of the file as its argument.
If new_name is the name of an existing file in the same folder then the function may either fail or override the existing file, depending on the specific system and library implementation.
Syntax:
int rename (const char *old_name, const char *new_name); Parameters: old_name : Name of an existing file to be renamed. new_name : String containing new name of the file.
Return:
Return type of function is an integer. If the file is renamed successfully, zero is returned. On failure, a nonzero value is returned.
Assume that we have a text file having name geeks.txt, having some content. So, we are going to rename this file, using the below C program present in the same folder where this file is present.

// C program to demonstrate use of rename() #include<stdio.h> int main() { // Old file name char old_name[] = "geeks.txt"; // Any string char new_name[] = "geeksforgeeks.txt"; int value; // File name is changed here value = rename(old_name, new_name); // Print the result if(!value) { printf("%s", "File name changed successfully"); } else { perror("Error"); } return 0; } |
Output:
If file name changed
File name changed successfully
OR
If file name not changed
Error: No such file or 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.
Recommended Posts:
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call function within function in C or C++
- Difference between virtual function and inline function in C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Importance of function prototype in C
- What happens when a function is called before its declaration in C?
- Default arguments and virtual function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

