BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strchr() Function with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: c-programming

The function strchr() searches the occurrence of a specified character in the given string and returns the pointer to it.

C strchr() Function

char *strchr(const char *str, int ch)

str – The string in which the character is searched.
ch – The character that is searched in the string str.

Return Value of strchr()

It returns the pointer to the first occurrence of the character in the given string, which means that if we display the string value of the pointer then it should display the part of the input string starting from the first occurrence of the specified character.

Example: strchr() function in C

In this example, we have a string and we are searching a character ‘u’ in the string using strchr() function. When we displayed the string value of the returned pointer of the function, it displayed the string starting from the character ‘u’, this is because the pointer returned by the function points to the character ‘u’ in the string.

#include <stdio.h>
#include <string.h>
int main () {
   const char str[] = "This is just a String"; 
   const char ch = 'u'; 
   char *p;
   p = strchr(str, ch);
   printf("String starting from %c is: %s", ch, p);
   return 0;
}

Output:

String starting from u is: ust a String

Related Posts:

  1. C – strcat() function
  2. C – strncat() function

Top Related Articles:

  1. C – loops in C programming with examples
  2. C – Pointer to Pointer (Double Pointer) with example
  3. Passing pointer to a function in C with example
  4. C strncpy() Function – C tutorial
  5. C – switch case statement in C Programming with example

Tags: C-Library, C-String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Roman Horváth says

    January 3, 2021 at 2:58 PM

    It is not written anywhere what happens when a character is not found. Now I know – the function returns null. This is important information…

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap