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.