C programming stdio.h function - int rename(const char *old_filename, const char *new_filename)

In C programming, the rename() function is defined in the stdio.h header file and is used to rename a file. The function takes two arguments: a pointer to a null-terminated string containing the name of the old file, and a pointer to a null-terminated string containing the new name for the file:

int rename(const char *old_filename, const char *new_filename);
Sour‮i.www:ec‬giftidea.com

The rename() function returns 0 if the file is successfully renamed, or a non-zero value if an error occurs. An error can occur if the old file does not exist or cannot be renamed, or if the new file already exists.

Here's an example of how to use the rename() function to rename a file:

#include <stdio.h>

int main() {
    char *old_filename = "example.txt";
    char *new_filename = "renamed.txt";
    int result = rename(old_filename, new_filename);

    if (result == 0) {
        printf("%s was renamed to %s.\n", old_filename, new_filename);
    } else {
        printf("Error renaming %s.\n", old_filename);
    }

    return 0;
}

In the above example, the rename() function is used to rename the file named "example.txt" to "renamed.txt". If the file is successfully renamed, a message is printed to the console indicating the old and new filenames. If an error occurs, a different message is printed to the console.