Reversing Strings

Why Is strrev() Not Working in C?

strrev() is not part of standard C, so many compilers do not provide it. Learn why it fails, how Windows names it _strrev(), and a portable fix.

On this page
  1. The short answer
  2. Why the build fails
  3. Windows and _strrev
  4. Reversing in place needs a writable array
  5. A portable C version
  6. Using C++ instead
  7. Is there a string library in C?

The short answer

strrev() is not part of standard C, so many compilers do not provide it. Some systems do offer it, usually through a vendor’s C library, which is why code that uses it can build on one machine and fail on another.

Why the build fails

The build stops with an error about strrev. In C++, the compiler reports that strrev is not declared, and this is always an error. In C, you may see either an error about an implicit declaration, which recent GCC versions report by default, or an undefined reference at link time. Typical Linux C libraries do not provide strrev, so the fix is to stop calling it rather than adding a header.

Windows and _strrev

Microsoft’s C runtime provides a reversing function named _strrev, declared in <string.h>. Code that calls _strrev builds with that runtime, but it may not build on Linux or macOS.

Reversing in place needs a writable array

strrev changes the characters inside the array it receives. The array must be writable and must end with a null character. A string literal is not writable:

char *word = "hello";  /* points to a string literal */
strrev(word);          /* undefined behavior: a string literal must not be changed */

Copy the literal into an array first:

char word[] = "hello";  /* a writable copy of the characters */

A portable C version

Instead of relying on strrev, write a small function that uses only standard headers:

#include <stdio.h>
#include <string.h>

void reverse_string(char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len / 2; i++) {
        char tmp = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = tmp;
    }
}

int main(void) {
    char word[] = "hello";
    reverse_string(word);
    printf("%s\n", word);  /* olleh */
    return 0;
}

Using C++ instead

The C++ standard library does not include strrev() either. In C++, use std::reverse from <algorithm>, which works on std::string.

Is there a string library in C?

Yes. <string.h> is the standard header for string functions such as strlen and strcmp. strrev is not one of them.

The complete guide to reversing strings

How to Reverse a String in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

std::array in C++

A complete guide to std::array in C++: declaring, initializing, reading elements, looping, passing to functions, and how it compares to C-style arrays.

std::array 5 min read

Sets in C++: How std::set Works

Learn how C++ std::set stores unique values in sorted order, with examples of insert, find, erase, custom comparators, and how it differs from hash sets.

std::set 5 min read