if和strcmp 的深入理解

在 C 语言中,if 条件中为 0 或假时,代码块将不会执行。而 !0 的结果是真,因此代码块将会被执行。
简单记忆: !0 和 true 执行! 0跳过,执行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
		

==总结:==实际应用只需要 相等或者不相等 两种情况;所以用上“ ”比较多见

猜你喜欢

转载自blog.csdn.net/m0_73344394/article/details/130426678