Introduction to C language: compare the size of three numbers

 if(a>b)   
	{
	max=a;
	}
    else        
	{
    max=b;
	}
    if(c>max) 
	{
    max = c;
	}

This is the easiest method to understand. Based on the previous judgment of the size of the two numbers, nest an if statement in the if statement, and obtain the maximum number after multiple judgments.

In this way, the referenced function can be changed to max(a,b,c) in the referenced function or the function comparing two numbers can be changed to max=max(max(a,b),c) in the main program.

The following introduces a ternary operator, which will further simplify the program and make it look more intuitive.

Its general form is as follows:

Expression1? Expression2 : Expression 3;

? The value of expression is determined by expression1. If expression1 is true, expression2 is evaluated and the result is the value of the entire ? expression. If expression1 is false, expression3 is evaluated and the result is the value of the entire ? expression.

The procedure is as follows:

#include<stdio.h>
int main()
{
	int max;
	int a,b,c;
	printf("请输入三个数:");
	scanf("%d %d %d",11&a,&b,&c);
	max=a>b?a:b;
	max=c>max?c:max;
	printf("Max is %d",max);
	return 0; 
}

If you have any questions, please leave a comment and I will answer it for you.

Guess you like

Origin blog.csdn.net/samxiaoguai/article/details/78398597