Strchr( ) function, Syntax, Example – Echelon Neuron C User Manual

Page 158: Strcmp( ) function

Advertising
background image

138

Functions

strchr( )

Function

The strchr( ) function searches the string

s

for the first occurrence of the

character

c

. If the string does not contain

c

, the strchr( ) function returns the

null pointer. The NUL character terminator ('\0') is considered to be part of the

string, thus strchr(s,'\0') returns a pointer to the NUL terminator. See also

strcat( ), strcmp( ), strcpy( ), strlen( ), strncat( ), strncmp( ), strncpy( ), and
strrchr( ).

Syntax

#include <string.h>

char *strchr (const char *

s

, char

c

);

Example

#include <string.h>

void f(void)
{
char

buf[20];

char

*p;


strcpy(buf, "Hello World");

p = strchr(buf, 'o');

// Assigns &(buf[4]) to p

p = strchr(buf, '\0');

// Assigns &(buf[11]) to p

p = strchr(buf, 'x');

// Assigns NULL to p

}

strcmp( )

Function

The strcmp( ) function compares the contents of the

s1

and

s2

strings, up to the

NUL terminator character in the shorter string. The function performs a case-

sensitive comparison. If the strings match identically, 0 is returned. When a

mismatch occurs, the characters from both strings at the mismatch are
compared. If the first string’s character is greater using an unsigned comparison,

the return value is positive. If the second string's character is greater, the return
value is negative.
The terminating NUL ('\0') character is compared just as any other character.

See also strcat( ), strchr( ), strcpy( ), strlen( ), strncat( ), strncmp( ), strncpy( ),
and strrchr( ).

Syntax

#include <string.h>

int strcmp (const unsigned char *

s1

, const unsigned char *

s2

);

Example

#include <string.h>

void f(void)

Advertising