In-depth understanding of if and strcmp

In C language, if the if condition is 0 or false, the code block will not be executed. And !0 evaluates to true, so the code block will be executed.
Simple to remember: !0 and true execute ! 0 skip, execute else

int main() {
    
    
	const char* str2 = "world";
	const char* str3 = "world";

	//equal  		if(!0); if(true); if(_stricmp(str2, str3)==0);	if (!_stricmp(str2, str3))
	//not equal		if(0);	if (_stricmp(str2, str3));	if(!_stricmp(str2, str3)==0)
	
	//结论1		 !0 和 true 执行! 0跳过执行else
	// 
	//结论2		if(_stricmp(str2, str3)==0)	equal  《---  if(0=0)	条件为真,执行	
	//			if(!_stricmp(str2, str3)==0) not equal.		《---  if(!0=0)	条件为假,跳过	
	// 
	//结论3		if (_stricmp(str2, str3))	not equal.		 《---  if(0)	条件为假,跳过	
	//			if (!_stricmp(str2, str3))	equal.		 《---  if(!0)	条件为真,执行	

	if(true)
	{
    
    
		printf("The strings are equal.\n");
	}
	else {
    
    
		printf("The strings are not equal.\n");
	}
	return 0;
}

strcmp

int main()
{
    
    
	char string1[] = "abc";
	char string2[] = "abc";
	char string3[] = "bcde";
	int a, b,c;
	a = strcmp(string1, string2);
	b = strcmp(string1, string3);
	c = strcmp(string3, string1);
	printf("%d %d %d", a, b,c);		//0  -1  1
	return 0;

}
		char string1[] = "abc";
		char string2[] = "abc";
		char string3[] = "bcde";
		int a, b, c;
		a = !strcmp(string1, string2);
		b = !strcmp(string1, string3);
		c = !strcmp(string3, string1);
		printf("%d %d %d", a, b, c);	// 1 0 0
		

==Summary:==Practical applications only need two situations of equality or inequality; so use ""More common

Guess you like

Origin blog.csdn.net/m0_73344394/article/details/130426678