From aldeid
Jump to navigation Jump to search


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


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.


C string to be compared.
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


C source Assembly
#include <stdio.h>
#include <string.h>

int main()
  char password[30];
  char expected[] = "veryeasy";
  printf("What is the password? ");
  scanf("%30s", password);
  if (strcmp (password, expected) == 0) {
  } else {
  return 0;
.text:00000000004005E6 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:00000000004005E6                 public main
.text:00000000004005E6 main            proc near
.text:00000000004005E6 expected        = byte ptr -30h
.text:00000000004005E6 var_28          = byte ptr -28h
.text:00000000004005E6 password        = byte ptr -20h
.text:00000000004005E6                 push    rbp
.text:00000000004005E7                 mov     rbp, rsp
.text:00000000004005EA                 sub     rsp, 30h
.text:00000000004005EE                 mov     rax, 7973616579726576h ; 'veryeasy'
.text:00000000004005F8                 mov     qword ptr [rbp+expected], rax
.text:00000000004005FC                 mov     [rbp+var_28], 0
.text:0000000000400600                 mov     edi, offset format ; "What is the password? "
.text:0000000000400605                 mov     eax, 0
.text:000000000040060A                 call    _printf
.text:000000000040060F                 lea     rax, [rbp+password]
.text:0000000000400613                 mov     rsi, rax
.text:0000000000400616                 mov     edi, offset a30s ; "%30s"
.text:000000000040061B                 mov     eax, 0
.text:0000000000400620                 call    ___isoc99_scanf
.text:0000000000400625                 lea     rdx, [rbp+expected]
.text:0000000000400629                 lea     rax, [rbp+password]
.text:000000000040062D                 mov     rsi, rdx        ; s2
.text:0000000000400630                 mov     rdi, rax        ; s1
.text:0000000000400633                 call    _strcmp
.text:0000000000400638                 test    eax, eax
.text:000000000040063A                 jnz     short wrong_password
.text:000000000040063C                 mov     edi, offset s   ; "OK"
.text:0000000000400641                 call    _puts
.text:0000000000400646                 jmp     short loc_400652
.text:0000000000400648 ; ---------------------------------------------------------------------------
.text:0000000000400648 wrong_password:
.text:0000000000400648                 mov     edi, offset aKo ; "KO"
.text:000000000040064D                 call    _puts
.text:0000000000400652 loc_400652:
.text:0000000000400652                 mov     eax, 0
.text:0000000000400657                 leave
.text:0000000000400658                 retn
.text:0000000000400658 main            endp