strcmp() — 스트링 비교

형식

#include <string.h>
int strcmp(const char *string1, const char *string2);

언어 레벨

ANSI

스레드세이프

설명

strcmp() 함수는 string1string2를 비교합니다. 함수는 널로 끝나는 스트링에서 작동합니다 함수에 대한 스트링 인수는 스트링 끝을 나타내는 널 문자(\0)를 포함해야 합니다.

리턴값

strcmp() 함수는 다음과 같이 두 스트링 사이의 관계를 나타내는 값을 리턴합니다.

표 1. strcmp()의 리턴값
의미
0보다 작음 string1string2보다 작음
0 string1string2와 같음
0보다 큼 string1string2보다 큼

이 예는 strcmp()을 사용하여 main()에 전달되는 두 개의 스트링을 비교합니다.
#include <stdio.h>
#include <string.h>
 
int main(int argc, char ** argv)
{
  int result;
 
  if ( argc != 3 )
  {
    printf( "Usage: %s string1 string2\n", argv[0] );
  }
  else
  {
 
    result = strcmp( argv[1], argv[2] );
 
    if ( result == 0 )
      printf( "\"%s\" is identical to \"%s\"\n", argv[1], argv[2] );
    else if (result < 0)
      printf( "\"%s\" is less than \"%s\"\n", argv[1], argv[2] );
    else
      printf( "\"%s\" is greater than \"%s\"\n", argv[1], argv[2] );
  }
}
 
/******************  If the input is the strings  ***********************
**********  "is this first?" and "is this before that one?",  ***********
******************  then the expected output is:  *********************
 
"is this first?" is greater than "is this before that one?"
**********************************************************************/