strchr C Library Function

The function char *strchr(const char *str, int c); searches for the first occurrence of character c in the string pointed by str.

Function prototype of strchr

char *strchr(const char *str, int c);
  • str : This is the string to be scanned for c.
  • c : Character to be searched.

Return value of strchr

This function returns a pointer to the first occurrence of the character c in the string str. If character c is not found in str, it returns NULL.

C program using strchr function

The following program shows the use of strchr function to search a character in a string.

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

int main() {
   char string[100];
   char ch, *ptr;
   printf("Enter any string\n");
   printf("Enter any character to search\n");
   scanf("%c", &ch);
   ptr = strchr(string, ch);

   if(NULL == ptr){
      printf("'%c' not found in string \"%s\"\n", ch, string);
   } else {
      printf("String after %c is \"%s\"\n", ch, ptr);

Enter any character to search
String after a is "ashCourse"
Enter any string
Enter any character to search
'z' not found in string "TechCrashCourse"