终端未显示任何输出(空白)

// if the ages of 3 people are put telling which one is youngest//

#include<stdio.h>

int main()
{
    int a1,a2,a3;

    printf("enter the age of ram,shyam and ajay");
    scanf("%d,%d,%d",&a1,&a2,&a3);

    if(a1>a2>a3)
        printf("%d,%d,%d",a1,a2,a3);
    if(a3>a2>a1)
        printf("%d,%d,%d",a3,a2,a1);
    if(a2>a1>a3)
        printf("%d,%d,%d",a2,a1,a3);
    if(a2>a3>a1)
        printf("%d,%d,%d",a2,a3,a1);
    if(a1>a3>a2)
        printf("%d,%d,%d",a1,a3,a2);
    if(a3>a1>a2)
        printf("%d,%d,%d",a3,a1,a2);

    return 0;
}

我不确定您是否应该在1&>2&>3这样的if语句中编写3个表达式 如果我说错了,请纠正我

推荐答案

根据C标准(6.5.8关系运算符)

6运算符<;(小于)、>;(大于)、<;=(小于 或等于),以及gt=(大于或等于)shall yield 1 if the specified relation is true and 0 if it is false.其结果是 键入int.

并且从左到右对运算符进行判断,

所以如果像这样的声明

if(a1>a2>a3)

相当于

if( ( a1 > a2 ) > a3 )

根据a1是否大于a2,你可以 Select

if( ( 1 ) > a3 )

if( ( 0 ) > a3 )

Instead you need to include in the expression logical AND operat或 like

if( a1 > a2 && a1 > a3 )

但是,如果您要这样做,如果至少有两个年龄相等,程序将不会输出任何内容.

也是在这个scanf的呼唤中

scanf( "%d,%d,%d", &a1, &a2, &a3 );

it is better to remove commas between the conversion specifiers. Let the user will enter ages separated by spaces like f或 example

20 22 21

20
22
21

If you want to output the ages in the descending 或der you could write f或 example

#include <stdio.h>

int main( void )
{
    int a1, a2, a3;

    printf( "enter the age of ram,shyam and ajay: " );
    scanf( "%d %d %d", &a1, &a2, &a3 );

    if ( a1 < a2 )
    {
        int tmp = a1;
        a1 = a2;
        a2 = tmp;
    }

    if ( a2 < a3 )
    {
        int tmp = a2;
        a2 = a3;
        a3 = tmp;
    }
 
    if ( a1 < a2 )
    {
        int tmp = a1;
        a1 = a2;
        a2 = tmp;
    }

    printf( "%d, %d, %d\n", a1, a2, a3 );
}

C++相关问答推荐

Mbed TLS:OAEP的就地en—/decryption似乎不起作用'

ATmega328P USART发送字符重复打印

使用单个字节内的位字段

C中是否有语法可以直接初始化一个常量文本常量数组的 struct 成员?

C:二进制搜索和二进制插入

正在try 将文件/文件夹名从目录 struct 存储到链接列表

不会停在空格或换行符上的错误

从纯C中访问通用项对话框

为什么GDB/MI进程的FIFO循环中有read()阻塞

对于C中给定数组中的每个查询,如何正确编码以输出给定索引范围(1到N)中所有数字的总和?

如何在STM8项目中导入STM8S/A标准外设库(ST VisualDeveloper)?

用C语言计算文本文件中的整数个数

如何在C中使数组变量的值为常量?

如何在C++中安全地进行浮点运算

将回调/基于事件的C API转换为非回调API

运行时错误:在索引数组时加载类型为';char';`的空指针

如何将两个uint32_t值交织成一个uint64_t?

共享目标代码似乎不能在Linux上的进程之间共享

为什么argc和argv即使在主函数之外也能工作?

访问未对齐联合的成员是否为未定义行为,即使被访问的成员已充分对齐?