The strstr() function locates the first occurrence of a substring inside a larger C string. It is one of the most common search tools in <string.h> when you need to find words, file extensions, or any multi-character pattern.
01
Find sub
First match.
02
char*
Pointer.
03
NULL
Not found.
04
Index
p - str.
05
Case
Sensitive.
06
vs strchr
One char.
Fundamentals
Definition and Usage
strstr searches a haystack string for a needle substring. When the needle appears, the function returns a pointer to the first character of the match inside the haystack—not a copy of the substring.
For example, searching "simple" in "This is a simple example." returns a pointer to the 's' in "simple example."
💡
Beginner Tip
strchr finds a single character; strstr finds a whole substring. Always test for NULL before using the returned pointer—printing or dereferencing NULL crashes your program.
strstr matches the multi-byte suffix ".pdf". strrchr finds the last '/' so you can isolate the filename. Together they cover common path parsing tasks.
Applications
🚀 Common Use Cases
Keyword detection — check whether user input contains a command or flag.
File extensions — locate ".txt", ".json", or similar suffixes.
Config parsing — find "key=" before reading a value.
Simple replace prep — find a match, then copy or overwrite surrounding text manually.
🧠 How strstr() Works
1
Start at haystack[0]
Try to match needle at the current position.
Init
2
Compare bytes
If needle matches, return pointer here. Otherwise advance one byte in haystack.
Scan
3
End of haystack
If no match before '\0', return NULL.
Fail
=
🔍
char* or NULL
Address inside haystack, or no match.
Important
📝 Notes
Case-sensitive byte comparison—"ABC" and "abc" do not match.
The returned pointer aliases haystack; do not free it separately.
Empty needle ("") returns haystack per the C standard.
Do not pass NULL for either argument—behavior is undefined.
For bounded buffers (not guaranteed null-terminated), prefer memmem on POSIX or manual length-limited search.
Performance
⚡ Optimization
Standard library implementations often use efficient algorithms (such as Two-Way search) for long needles. If you call strstr repeatedly on the same haystack, save the result pointer instead of searching again. For very large data or binary blobs, consider specialized search routines with explicit lengths.
Wrap Up
Conclusion
strstr() is the standard way to find the first occurrence of a substring in C. Check for NULL, use pointer arithmetic for indices, and combine it with strchr or strrchr when you need single-character delimiters.
Next, explore strpbrk() to locate the first character from a set.
Confuse return pointer with a newly allocated string
Call with NULL arguments
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strstr()
Use these points when searching for substrings in C.
5
Core concepts
🔍01
First Match
Left to right.
Basics
📝02
NULL Safe
Check result.
Safety
🔢03
Index
p - hay.
Pointer
📈04
Multi-byte
Whole sub.
Use case
💬05
vs strchr
One char.
Compare
❓ Frequently Asked Questions
strstr() searches haystack for the first occurrence of needle (a substring). It returns a pointer to the start of the match inside haystack, or NULL if needle is not found.
char* strstr(const char* haystack, const char* needle); Include <string.h>. haystack is the string to search. needle is the substring to locate.
It returns NULL. Always check the result before dereferencing or printing it.
Yes. strstr() compares bytes exactly. "Hello" and "hello" are different. For case-insensitive search on POSIX systems, strcasestr() may be available as an extension.
If char* p = strstr(haystack, needle) is non-NULL, the zero-based index is (size_t)(p - haystack). Both pointers must refer to the same array.
strstr() returns haystack—the empty string is considered to match at the beginning. This is defined by the C standard.
Did you know?
C++ offers std::string::find, but in C, strstr remains the idiomatic choice for null-terminated strings. On GNU/Linux, strcasestr() provides a case-insensitive variant—it is not part of standard C, so wrap it in #ifdef if you need portable code.