Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
round() Function in C
Next article icon

rename function in C

Last Updated : 21 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The rename() function is used to rename a file in C. It changes the name of the file from old_name to new_name without modifying the content present in the file. It is defined inside <stdio.h> header file.

In this article, we will learn how to rename a file using the rename() function in C programming language.

Syntax of rename()

int rename (const char *old_name, const char *new_name);

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.

Parameters

  • old_name: String that represents the name of an existing file to be renamed.
  • new_name: String containing the new name of the file.

Return Value

The return type of the function is an integer.

  • If the file is renamed successfully, zero is returned and it prints a success message.
  • On failure, a nonzero value is returned and it prints an error message using perror().

Example of rename()

Assume that we have a text file having the 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.

filename before running program 

C Program to Demonstrate the use of rename() Function

C




// 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 is not present Error: No such file or directory

filename after running code



Next Article
round() Function in C

A

AKASH GUPTA 6
Improve
Article Tags :
  • C Language
  • C++
  • C-File Handling
  • C-Library
  • cpp-file-handling
  • CPP-Library
Practice Tags :
  • CPP

Similar Reads

  • raise() function in C++
    csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored. Syntax: int raise ( int signal_ ) Parameter: The f
    3 min read
  • strlen() function in c
    The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file. It doesn't count the null character '\0'. Syntax of C strlen() The syntax of strlen() function in C is as follows: size_t strlen(const char* str);Parameters The strlen() func
    1 min read
  • strnset() function in C
    The strnset() function is a builtin function in C and it sets the first n characters of a string to a given character. If n is greater than the length of string, the length of string is used in place of n. Syntax: char *strnset(const char *str, char ch, int n); Parameters: str: This is the original
    2 min read
  • round() Function in C
    In the C language, the <math.h> header file contains the Standard Math Library that provides various mathematical functions, including the round() function. In this article, we will see how to use the round() function in C. What is round() in C?C round() is a built-in library function that rou
    3 min read
  • moveto() function in C
    The header file graphics.h contains moveto() function which changes the current position to (x, y) Syntax : void moveto(int x, int y); Examples : Input : x = 70, y = 40 Output : Input : x = 50, y = 80 Output : Below is the implementation of moveto() function: // C Implementation for moveto() #includ
    2 min read
  • strrev() function in C
    The strrev() function is a built-in function in C and is defined in string.h header file. The strrev() function is used to reverse the given string. Syntax: char *strrev(char *str);Parameter: str: The given string which is needed to be reversed.Returns: This function doesn't return anything but the
    2 min read
  • lineto() function in C
    The header file graphics.h contains lineto() function which draws a line from current position to the point(x,y). Note : Use getx() and gety() to get the current position. Syntax : lineto(int x, int y); where, (x, y) are the coordinates upto which the line will be drawn from previous point. CASE 1 :
    2 min read
  • main Function in C
    The main function is the entry point of a C program. It is a user-defined function where the execution of a program starts. Every C program must contain, and its return value typically indicates the success or failure of the program. In this article, we will learn more about the main function in C.
    5 min read
  • Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
  • Function Pointer in C++
    Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences