C strrchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strrchr() searches a C-string for the last matching character. It is useful for the last '/' in a path or the last '.' before an extension.

You pass the string and the character. Both are required. The result is a pointer into the same string, or NULL when the character is missing. Include <string.h>.

Syntax

Pass the string and the character. Start with this call.

C
strrchr(path, '/');
CallWhat it does
strrchr(path, '/')Looks for the last '/' in path.

Include <string.h>. Check for NULL. strchr() stops at the first match.

Library declarations
C
char *strrchr(const char *str, int c);

Parameters

ParameterDescription
strRequired. The C-string to search.
chRequired. The character to find, written in single quotes.

Return Value

Return ValueDescription
PointerPoints to the last match inside the same string.
NULLReturned when the character is not found.

C Examples

Example 1 — Find the Last Match

Search for the last 'a' and print from that position. Earlier matches are ignored.

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

int main(void) {
    const char *str = "banana";
    char *p = strrchr(str, 'a');

    if (p != NULL) {
        printf("last 'a' at index %td\n", p - str);
        printf("suffix: %s\n", p);
    } else {
        printf("not found\n");
    }

    return 0;
}

How It Works

  1. Scan the whole string. Every 'a' updates the remembered match.
  2. Last one wins. In "banana", that is index 5.
  3. Check NULL first. Only then subtract pointers or print.
  4. Contrast with strchr. strchr would stop at index 1.

Example 2 — Get the Filename from a Path

Find the last slash, then print the basename that follows it.

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

int main(void) {
    const char *path = "/home/sam/notes.txt";
    char *slash = strrchr(path, '/');

    if (slash != NULL) {
        printf("file = %s\n", slash + 1);
    } else {
        printf("file = %s\n", path);
    }

    return 0;
}

How It Works

  1. Find the last '/'. That separates directories from the file name.
  2. Skip the slash. slash + 1 points at "notes.txt".
  3. Handle no slash. If NULL, the whole string is already a basename.
  4. Windows tip. Paths may use '\\'—search for that character instead when needed.

Example 3 — Read a File Extension

Use the last '.' so names like file.tar.gz give gz, not tar.gz.

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

int main(void) {
    const char *name = "archive.tar.gz";
    char *dot = strrchr(name, '.');

    if (dot != NULL && *(dot + 1) != '\0') {
        printf("ext = %s\n", dot + 1);
    } else {
        printf("no extension\n");
    }

    return 0;
}

How It Works

  1. Last dot matters. strrchr skips the earlier '.' in tar.
  2. Move past the dot. dot + 1 is the extension text.
  3. Guard empty tails. A trailing '.' alone is not a useful extension.
  4. First-dot would be wrong. strchr would point at ".tar.gz".

Frequently Asked Questions

strrchr() finds the last matching character in a C-string and returns a pointer to it, or NULL.
Both are required. str is the C-string, and ch is the character in single quotes.
A pointer to the last match, or NULL when the character is missing.
Include <string.h>.
strchr() stops at the first match. strrchr() keeps the last match.

Next: strrev()

Reverse the characters of a C-string in place.

strrev() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful