Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – strcmp()

C Library Function - strcmp()

C Standard Library - <string.h> C Standard Library -

Description

C Library Function **int strcmp(const char str1, const char str2) Puts the str1 compares the string pointed to by str2 with the string pointed to by .

Declaration

Below is the declaration of the strcmp() function.



int strcmp(const char *str1, const char *str2)

Parameters

Return Value

The function returns the following values:

Example

The following example demonstrates the use of the strcmp() function.

Example

#include

<

stdio.h

#include

<

string.h

int

main

(

)

{

char

str1

[

15

]

;

char

str2

[

15

]

;

int

ret

;

strcpy

(

str1

,

"

abcdef

"

)

;

strcpy

(

str2

,

"

ABCDEF

"

)

;

ret

=

strcmp

(

str1

,

str2

)

;

if

(

ret

<

0

)

{

printf

(

"

str1 is less than str2

"

)

;

}

else

if

(

ret

0

)

{

printf

(

"

str1 is greater than str2

"

)

;

}

else

{

printf

(

"

str1 equals str2

"

)

;

}

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



str1 大于 str2

C Standard Library - <string.h> C Standard Library -