The strrchr() function finds the last occurrence of a character in a C-string. It is the reverse-direction partner of strchr()—perfect for file paths, extensions, and any task where the final match matters.
01
Last match
Right to left.
02
Returns ptr
Or NULL.
03
Suffix view
From match.
04
Read-only
No modify.
05
string.h
Standard C.
06
vs strchr
First match.
Fundamentals
Definition and Usage
strrchr stands for string reverse char. It scans from the end of the string backward (conceptually) and returns a pointer to the last byte equal to c. Printing result with %s shows the suffix starting at that character.
Common jobs include finding the last path separator in /home/user/docs/report.pdf, the last dot before an extension, or the final delimiter in a log line.
💡
Beginner Tip
If you need the first match, use strchr. If you need the last, use strrchr. For binary buffers with a known length (not necessarily C-strings), consider memrchr where available.
Foundation
📝 Syntax
Standard C declaration:
C
char *strrchr(const char *str, int c);
Parameters
str — null-terminated string to search.
c — character to find, passed as int (e.g. 'o' or '/').
Return Value
Pointer to the last occurrence of c in str.
NULL if c does not appear (except c == '\0', which always matches the terminator).
Header
#include <string.h>
Cheat Sheet
⚡ Quick Reference
str
c
Result pointer at
Printing %s shows
"Hello, C Programming!"
'o'
last o in Programming
"ogramming!"
"/home/user/file.txt"
'/'
final slash
"/file.txt"
"archive.tar.gz"
'.'
last dot
".gz"
index
if p != NULL
p - str (zero-based position)
Last char
strrchr(str, c)
Final match
First char
strchr(str, c)
Initial match
Suffix
printf("%s", p)
From match
Check
if (p != NULL)
Before use
Hands-On
Examples Gallery
Compile with gcc strrchr.c -std=c11 -o strrchr. Each example checks for NULL before using the returned pointer.
📚 Getting Started
Find the final occurrence of a character and print the suffix.
Example 1 — Find the Last 'o'
From "Hello, C Programming!", locate the final letter o.
C
#include <stdio.h>
#include <string.h>
int main(void) {
const char *text = "Hello, C Programming!";
char searchChar = 'o';
char *result = strrchr(text, searchChar);
if (result != NULL) {
printf("Last occurrence of '%c': %s\n", searchChar, result);
} else {
printf("'%c' not found in the string.\n", searchChar);
}
return 0;
}
📤 Output:
Last occurrence of 'o': ogramming!
How It Works
The first o is in Hello, but strrchr skips ahead to the o in Programming. The returned pointer starts a new C-string view: everything from that byte to the end.
Example 2 — Find the Last Path Separator
Split a Unix-style path into directory and filename at the final /.
slash + 1 skips past the separator to the basename. The length of the directory prefix is slash - path bytes—printed here with the %.*s precision format.
📈 Practical Patterns
Extensions, comparison with strchr, and missing characters.
Example 3 — Get the File Extension
Use the last . to isolate an extension (simple cases only).
strchr stops at the first hyphen; strrchr at the second. Choose based on whether you care about the opening or closing delimiter.
Example 5 — Character Not Found
Handle NULL when the search character never appears.
C
#include <stdio.h>
#include <string.h>
int main(void) {
const char *word = "CodeToFun";
char *p = strrchr(word, 'z');
if (p == NULL) {
printf("'z' does not appear in \"%s\"\n", word);
} else {
printf("Found at: %s\n", p);
}
return 0;
}
📤 Output:
'z' does not appear in "CodeToFun"
How It Works
When no match exists, dereferencing the return value would crash. Always branch on NULL first.
Applications
🚀 Common Use Cases
File paths — find the last / or \ before the filename.
Extensions — locate the final . in a simple filename.
Log parsing — split at the last delimiter in a line.
URL handling — isolate the path segment after the final slash.
Teaching pointer views — suffix strings without copying memory.
🧠 How strrchr() Works
1
Find string end
Locate the terminating '\0'.
Length
2
Scan backward
Compare each byte to c from end toward start.
Search
3
Return last hit
Pointer to final match, or NULL if none.
Result
=
🔍
char* or NULL
Suffix view from the last matching character.
Important
📝 Notes
Case-sensitive—'a' and 'A' differ.
Return pointer points intostr; do not free() it.
strrchr(str, '\0') returns the terminator (same as end of string).
For Windows paths, also search '\\' or normalize separators first.
Not for binary blobs unless they are valid null-terminated C-strings.
Performance
⚡ Optimization
Standard library implementations scan at most once from the end. If you only need the string length, use strlen directly rather than strrchr(str, '\0'). For repeated searches on the same long string, cache the last known position when parsing incrementally.
Wrap Up
Conclusion
strrchr() finds the last occurrence of a character and returns a pointer into the original string. Use it for path basenames, extensions, and suffix extraction—always check for NULL first.
Next, learn about the non-standard strrev() function for reversing strings in place.
Assume the last dot is always the “real” extension
Modify memory through a const char* source
Use strrchr on non-null-terminated data
Confuse last match with first match—pick the right function
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strrchr()
Use these points when the final character match matters.
5
Core concepts
🔍01
Last match
Final char.
Basics
📝02
NULL safe
Check first.
Safety
🔢03
Suffix view
Print %s.
Pattern
📈04
vs strchr
First match.
Compare
💬05
Paths
Last / .
Use case
❓ Frequently Asked Questions
strrchr() searches a null-terminated string for the last occurrence of a character. It returns a pointer to that character inside the string, or NULL if the character is not found.
char* strrchr(const char* str, int c); Include <string.h>. str is the string to search. c is the character to find, passed as int (often a char literal like '/').
strchr() returns a pointer to the first match scanning left to right. strrchr() returns a pointer to the last match. Use strrchr for the final '/' in a path or the last '.' before a file extension.
It returns NULL. Always check before dereferencing or using pointer subtraction.
Yes. strrchr(str, '\0') returns a pointer to the terminating null byte at the end of the string—equivalent to str + strlen(str). Searching for '\0' is rarely needed in application code.
Yes. 'o' and 'O' are different. For case-insensitive last-match search, walk the string yourself with tolower() comparisons or normalize case first.
Did you know?
GNU systems also provide memrchr, which searches a memory block of known length for a byte—useful when data is not null-terminated. Standard C gives you strrchr for C-strings and memchr for forward scans in fixed-size buffers.