Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

index - First occurrence of a character in a string

&pagelevel(4)&pagelevel

Definition

#include <string.h>

char *index(const char *s, int c);

index searches for the first occurrence of character c in string s and returns a pointer to the
located position in s if successful.

The terminating null byte (\0) is also treated as a character.

Return val.

Pointer to the position of c in string s

if successful.

NULL pointer

if c is not contained in string s.

Note

Example

The index and strchr functions are equivalent.

Find the first ’f’:

#include <string.h>
#include <stdio.h>
int main(void)
{
   char *s = "What ffun in the sun!";
   printf("%s\n", s);
   printf("Where is the error? %s\n", index(s, 'f'));
   return 0;
}

See also

rindex, strchr