User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:string.h:strcmp

strcmp

#include <string.h>
//#include <windows.h> /* uncomment this for Windows */
 
    int strcmp (const char *s1, const char *s)

Description

strcmp Compares 2 C strings.

If both strings are identical, the return value of strcmp is 0.
If the string s1 less than s2, the return value is less than 0,
and s1 is greater than s2, then the return value greater than 0

C Example Code

/* strcmp example code
 * http://code-reference.com/c/stdio.h/strcmp 
 */
 
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
#include <string.h>
 
int main( void )
{
 
char string1[5]="test";
char string2[6]="test2";
char string3[6]="test2";
 
/* string1 and string2 not compare */
if ( strcmp( string1, string2 ) == 0 ){
        printf("same\n");
    } else { 
        printf("different\n");
    }
 
/* string1 and string2 not compare */
if ( strcmp( string2, string3 ) == 0 ){
        printf("same\n");
    } else { 
        printf("different\n");
    }
 
return 0;
}

Output of strcmp example

  user@host:~$ ./strcmp 
  different
  same

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/string.h/strcmp.txt · Last modified: 2024/02/16 01:06 (external edit)

Impressum Datenschutz