C++ strchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strchr() searches a C-string for one character and stops at the first match. Write the character in single quotes, such as '@'.

Both arguments are required. If the character is found, you get a pointer to that spot inside the same string. If it is missing, the result is nullptr. Check for that before you use the pointer. Include <cstring>.

Syntax

Pass the string, then the character in single quotes.

C++
strchr("user@mail", '@');
CallWhat it does
strchr("user@mail", '@')Points at the first @ inside the string.
Library declarations
C++
const char* strchr(const char* str, int ch);

Parameters

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

Return Value

Return ValueDescription
PointerPoints at the first matching character inside the same string.
nullptrThe character is not in the string.

C++ Examples

Example 1 — Basic strchr()

Find the first 'C' in a greeting.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* text = "Hello, C++!";
    const char* found = strchr(text, 'C');

    if (found != nullptr) {
        std::cout << "Found: " << found << '\n';
    } else {
        std::cout << "Not found.\n";
    }
    return 0;
}

How It Works

  1. strchr scans left to right until it sees 'C'.
  2. The returned pointer starts at that character.
  3. Printing the pointer shows the rest of the string from there.

Example 2 — Character Not Found

Missing characters return nullptr.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* email = "[email protected]";

    const char* at = strchr(email, '@');
    const char* hash = strchr(email, '#');

    if (at != nullptr) {
        std::cout << "Has @: yes\n";
    }

    if (hash == nullptr) {
        std::cout << "Has #: no\n";
    }
    return 0;
}

How It Works

  1. '@' exists, so at is not nullptr.
  2. '#' is missing, so hash is nullptr.
  3. Always test before using the pointer.

Example 3 — Get the Index

Subtract pointers to learn the zero-based position.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* path = "report.pdf";
    const char* dot = strchr(path, '.');

    if (dot != nullptr) {
        std::size_t index = static_cast<std::size_t>(dot - path);
        std::cout << "Dot at index: " << index << '\n';
        std::cout << "Extension: " << dot << '\n';
    }
    return 0;
}

How It Works

  1. dot - path is the distance from the start.
  2. That distance is the index of '.'.
  3. Printing dot shows ".pdf".

Frequently Asked Questions

strchr() finds the first time a character appears in a C-string.
str is the C-string to search. ch is the character to find.
A pointer to the first match inside the same string, or nullptr when the character is missing.
If the character is missing, there is no place to read. Using that pointer is unsafe.
No. strchr() finds the first match. strrchr() finds the last match.

Next: strcmp()

Compare two C-strings and see if they match or which comes first.

strcmp() 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.

8 people found this page helpful