C strchr() Function

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

Definition

In C, strchr() searches a C-string for the first matching character. Use single quotes for the character, such as '@'.

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
strchr(email, '@');
CallWhat it does
strchr(email, '@')Looks for the first '@' in email.

Include <string.h>. Check for NULL before you use the pointer. For the last match, use strrchr().

Library declarations
C
char *strchr(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 first match inside the same string.
NULLReturned when the character is not found.

C Examples

Example 1 — Find the First Match

Search for a character and print from that position. If it is missing, print a clear message instead of crashing.

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

int main(void) {
    const char *str = "Hi@ok";
    char *p = strchr(str, '@');

    if (p != NULL) {
        printf("found: %s\n", p);
    } else {
        printf("not found\n");
    }

    return 0;
}

How It Works

  1. Search left to right. strchr stops at the first '@'.
  2. Check NULL. A non-null p means a match exists.
  3. Print from the match. %s shows the suffix starting at '@'.
  4. Original string stays. strchr does not copy or modify the text.

Example 2 — Convert Pointer to Index

Subtract the start pointer to get a zero-based index—useful for reporting positions.

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

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

    if (p != NULL) {
        printf("index = %td\n", p - str);
    } else {
        printf("not found\n");
    }

    return 0;
}

How It Works

  1. First 'a' wins. In "banana", that is index 1.
  2. Pointer math. p - str is the number of characters from the start.
  3. Use %td. That format matches the ptrdiff_t result of pointer subtraction.
  4. Never subtract on NULL. Check the pointer first.

Example 3 — Split at a Marker Character

Find '@' in an email-like string, then print the user part and the domain part separately.

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

int main(void) {
    char email[] = "[email protected]";
    char *at = strchr(email, '@');

    if (at == NULL) {
        printf("no @ found\n");
        return 0;
    }

    *at = '\0';
    printf("user = %s\n", email);
    printf("host = %s\n", at + 1);

    return 0;
}

How It Works

  1. Locate the marker. strchr points at '@'.
  2. Cut the string. Writing '\0' over '@' ends the user part.
  3. Read the rest. at + 1 starts the host text.
  4. Need a writable buffer. A string literal cannot be modified this way—use an array.

Frequently Asked Questions

strchr() finds the first 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 first match, or NULL when the character is missing.
Include <string.h>.
strchr() stops at the first match. strrchr() keeps the last match.

Next: strrchr()

Find the last occurrence of a character in a C-string.

strrchr() 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