糯米文學吧

位置:首頁 > 計算機 > C語言

c語言strcmp的用法

C語言2.2W

函數 int stringcompare(char *source, char *target) 比較字符串 source 和 target,並根據 source 是否小於、等於或大於 target 的結果分別返回負整數、0或者整數。該返回值是 source 和 target 由前後逐字符比較時遇到的第一個不相等字符處的字符的差值。下面小編就來為大家介紹下c語言strcmp的.用法。

c語言strcmp的用法

#include

int stringcompare(char *source, char *target);

int main()

{

char str_a[] = "Welcome to ";

char str_b[] = "Welcome to ";

int wait, result;

result = stringcompare(str_b, str_a);

printf("After Function Call:");

printf("result is '%d'", result);

scanf("%d", &wait);

}

/* 根據 source 按照字典順序小於、等於或大於 target 的結果分別返回負整數、0或者整數 */

int stringcompare(char *source, char *target)

{

int i;

for(i = 0; source[i] == target[i]; i++)

{

if (source[i] == '