A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/cstring/strcmp/ below:

function

<cstring>

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

Compare two strings

Compares the C string str1 to the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.



Parameters
str1
C string to be compared.
str2
C string to be compared.

Return Value Returns an integral value indicating the relationship between the strings:
return value indicates <0 the first character that does not match has a lower value in ptr1 than in ptr2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in ptr1 than in ptr2
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>

int main ()
{
  char key[] = "apple";
  char buffer[80];
  do {
     printf ("Guess my favorite fruit? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  puts ("Correct answer!");
  return 0;
}

Output:
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!


See also
strncmp
Compare characters of two strings (function)
memcmp
Compare two blocks of memory (function)
strrchr
Locate last occurrence of character in string (function)
strspn
Get span of character set in string (function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4