strcmp()
Published Aug 25, 2021Updated Aug 13, 2022
Contribute to Docs
The strcmp()
function compares two strings and returns an integer value.
Syntax
#include <string.h>
strcmp(string1, string2);
At the top of the file, the <string.h>
header file needs to be included. The strcmp()
function compares strings string1
and string2
and returns an integer value.
- If the two strings are the same, return 0.
- If
string1
>string2
, return positive value. - If
string1
<string2
orstring1
is a substring ofstring2
, return negative value.
Example
#include <stdio.h>#include <string.h>int main() {char address1[20] = "575 Broadway";char address2[20] = "576 Broadway";if (strcmp(address1, address2) == 0) {printf("Address 1 and address 2 are equal.");}else {printf("Address 1 and address 2 are different.");}return 0;}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.