If you are someone who is interested in programming or is already a programmer, you must have come across the term strcmp. It is a commonly used function in the C programming language that is used to compare strings. In this article, we will take a closer look at strcmp and understand its usage in detail.
What is strcmp?
strcmp is a built-in function in the C programming language that is used to compare two strings. It takes two arguments, which are the strings that need to be compared, and returns an integer value. If both strings are the same, the function returns 0. If the first string is greater than the second string, it returns a positive value. If the first string is smaller than the second string, it returns a negative value.
Example:
Let's take an example to understand this better. Consider the following code: ``` #include #include int main() { char str1[20] ="Hello"; char str2[20] ="World"; int result = strcmp(str1, str2); printf("%d", result); return 0; } ``` In the code above, we have two strings, 'Hello' and 'World', and we are using the strcmp function to compare them. Since the first string is smaller than the second string, the function returns a negative value, which is -15. This value can be used to determine the order of the strings.
Usage of strcmp
Now that we know what strcmp is, let's take a look at its usage. The strcmp function is commonly used in programs that involve string manipulation. It is used to compare two strings to determine if they are the same or not. It can also be used to sort strings in alphabetical order.
Example:
Consider the following code: ``` #include #include int main() { char str1[20] ="Apple"; char str2[20] ="Banana"; char str3[20] ="Orange"; int result1 = strcmp(str1, str2); int result2 = strcmp(str2, str3); int result3 = strcmp(str1, str3); if(result1 < 0) printf("Apple comes before Banana"); else if(result1 > 0) printf("Banana comes before Apple"); else printf("Apple and Banana are the same"); if(result2 < 0) printf("\nBanana comes before Orange"); else if(result2 > 0) printf("\nOrange comes before Banana"); else printf("\nBanana and Orange are the same"); if(result3 < 0) printf("\nApple comes before Orange"); else if(result3 > 0) printf("\nOrange comes before Apple"); else printf("\nApple and Orange are the same"); return 0; } ``` In the code above, we have three strings, 'Apple', 'Banana', and 'Orange', and we are using the strcmp function to compare them. We are using the results of the function to determine the order of the strings. The output of the code will be: ``` Apple comes before Banana Banana comes before Orange Apple comes before Orange ```
Conclusion
In conclusion, the strcmp function is a powerful tool in the C programming language that is used to compare strings. It is commonly used in programs that involve string manipulation and sorting. By understanding the usage of strcmp, you can write more efficient and effective programs.
0 Response to "7+ Strcmp 使い方 References"
Posting Komentar