当前位置:网站首页>C language function strcmp() (compare two strings)
C language function strcmp() (compare two strings)
2022-06-13 03:01:00 【Python's path to becoming a God】
C Library function - strcmp()
List of articles
describe
C Library function int strcmp(const char *str1, const char *str2) hold str1 The string pointed to and str2 Compare the string pointed to .
Statement
Here is strcmp() Declaration of functions .
int strcmp(const char *str1, const char *str2)
Parameters
- str1 – The first string to compare .
- str2 – The second string to compare .
Return value
The return value of this function is as follows :
- If the return value is less than 0, said str1 Less than str2.
- If the return value is greater than 0, said str1 Greater than str2.
- If the return value is equal to 0, said str1 be equal to str2.
example
The following example demonstrates strcmp() Function usage .
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if (ret < 0)
{
printf("str1 Less than str2");
}
else if (ret > 0)
{
printf("str1 Greater than str2"); //str1 Greater than str2
}
else
{
printf("str1 be equal to str2");
}
return(0);
}
Be careful
Two strings are compared character by character from left to right ( Press ASCII Compare the value with the size ), Until a different character appears or \0 until . Such as :
- “A”<“B”
- “A”<“AB”
- “Apple”<“Banana”
- “A”<“a”
- “compare”<“computer”
Particular attention :strcmp(const char *s1,const char * s2) You can only compare strings , Can be used to compare two string constants , Or compare array and string constants , You can't compare other forms of parameters such as numbers .
ANSI The standard stipulates , The return value is a positive number , negative ,0 . And the exact value depends on different C Realized .
When two strings are not equal ,C The standard does not specify that the return value will be 1 or -1, Only positive and negative numbers are specified .
Some will put two characters ASCII The difference of codes is returned by the function value as the comparison result . But in any case, this basis can not be used as the process logic in the program .
边栏推荐
- Installing the IK word breaker
- [data analysis and visualization] key points of data drawing 11- precautions for radar chart
- Advanced stair climbing
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
- When the flutter runs the project, the gradle download fails, and the running gradle task 'assemblydebug' is always displayed
- Linked list: the first coincident node of two linked lists
- How to select fund products? What kind of fund is a good fund?
- MySQL 8.0 installation free configuration method
- Ijkplayer source code - setting option 2
猜你喜欢

Flutter reports an error type 'Int' is not a subtype of type 'string' wonderful experience

Wechat applet coordinate location interface usage (II) map interface

Vs Code modify default terminal_ Modify the default terminal opened by vs Code

2019 - sorting out the latest and most comprehensive IOS test questions (including framework and algorithm questions)

Prometheus install and register services

Applet image component long press to identify supported codes

The weight of the input and textarea components of the applet is higher than that of the fixed Z-index

Useful websites for writing papers and studying at ordinary times

Keil去掉烦人的ST-Link更新提示

Pytorch record: pytorch variables parameter and buffer. self. register_ buffer()、self. register_ parameter()
随机推荐
Spark UDF instance details
Sword finger offer2: queue summary
Wechat applet switch style rewriting
Advanced stair climbing
Linked list: reverse linked list
Ffmpeg principle
专业的数据库管理软件:Valentina Studio Pro for Mac
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
vant实现移动端的适配
OneNote User Guide (1)
A wechat app for shopping
String: number of substring palindromes
Pycharm and Anaconda ultra detailed installation and configuration tutorial
Delete the number of a range in the linked list
Using linked list to find set union
Ijkplayer source code - audio playback
[data analysis and visualization] key points of data drawing 10- construction of legend
Hash tables: metaphrases
小程序 input,textarea组件权重比fixed的z-index都高
Keil removes annoying st link update tips