C strstr() Function

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

Definition

In C, strstr() searches a C-string for the first matching substring. strstr("hello world", "world") points at the start of world.

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

Syntax

Pass the text to search, then the text to find. Start with this call.

C
strstr("hello world", "world");
CallWhat it does
strstr("hello world", "world")Finds the first world inside the sentence.

Include <string.h>. Check for NULL before you use the pointer. The search is case-sensitive.

Library declarations
C
char *strstr(const char *haystack, const char *needle);

Parameters

ParameterDescription
haystackRequired. The C-string to search.
needleRequired. The substring to find.

Return Value

Return ValueDescription
PointerPoints to the first match inside the haystack.
NULLReturned when the substring is not found.

C Examples

Example 1 — Find a Substring

Search for "world" inside a sentence and print from the match onward.

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

int main(void) {
    const char *hay = "hello world";
    char *p = strstr(hay, "world");

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

    return 0;
}

How It Works

  1. Call strstr. It scans hay for the first "world".
  2. Get a pointer. On success, p points inside hay at the match.
  3. Guard with NULL. Only print when a match exists.
  4. Print from the match. %s shows "world" (and anything after it).

Example 2 — Get the Match Index

Subtract pointers to turn the result into a zero-based index. Both must point into the same array.

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

int main(void) {
    const char *hay = "CodeToFun";
    char *p = strstr(hay, "To");

    if (p != NULL) {
        size_t index = (size_t)(p - hay);
        printf("index = %zu\n", index);
        printf("from  = %s\n", p);
    }

    return 0;
}

How It Works

  1. Find the substring. "To" starts at position 4 in "CodeToFun".
  2. Subtract pointers. p - hay is the index (bytes from the start).
  3. Cast to size_t. Safer for printing unsigned lengths.
  4. Reuse p. You still have a pointer to the rest of the string.

Example 3 — Handle a Missing Substring

When there is no match, strstr returns NULL. Always branch on that result.

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

int main(void) {
    const char *hay = "hello";
    char *p = strstr(hay, "xyz");

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

    return 0;
}

How It Works

  1. Search for "xyz". It does not appear in "hello".
  2. Get NULL. That is the standard “not found” result.
  3. Check first. Never print or dereference p when it is NULL.
  4. Tell the user. A clear message avoids crashes and confusion.

Frequently Asked Questions

strstr() finds the first matching substring and returns a pointer to it, or NULL.
Both are required. haystack is the text to search, and needle is the substring to find.
A pointer to the first match, or NULL when the substring is missing.
Include <string.h>.
Yes. Uppercase and lowercase letters must match.

Next: strtok()

Split a string into tokens using delimiter characters.

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