使用指针查找两个数中较大者的C程序

编写一个C程序,使用指针查找两个数中较大者。它允许用户输入两个整数值,然后我们将它们赋值给两个int类型的指针变量。在此示例中,我们使用了else if语句来查找两个指针数中较大的数。

#include <stdio.h>     

int main() {  

    int x, y, *p1, *p2;  

    printf("Please Enter Two different values\n");  
    scanf("%d %d", &x, &y);  
    
    p1 = &x;
    p2 = &y;

    if(*p1 > *p2) 
	{
         printf("The Larges = %d\n", *p1);  
    } 
	else if (*p2 > *p1)
	{ 
        printf("The Largest = %d\n", *p2); 
    } 
	else 
	{
		printf("Both are Equal\n");
    }
   
    return 0;  
} 
Please Enter Two different values
99
15
The Largest = 99


Please Enter Two different values
12
19
The Largest = 19


Please Enter Two different values
15
15
Both are Equal

这个C程序使用条件运算符和指针来查找两个数中较大的数。

#include <stdio.h>  
   
int main() 
{  
    int x, y, *p1, *p2, *largest;

    p1 = &x;
    p2 = &y;

    printf("Please Enter Two Different Values\n");  
    scanf("%d %d", p1, p2);  

    largest = (*p1 > *p2) ? p1 : p2;
    printf("The Largest of Two Number = %d\n", *largest);
    
    return 0;  
} 
C Program to Find the Largest of Two Numbers using a Pointer