Ternary operator usage error, "ERROR:void value not ignored as it ought to be"

Ternary operator usage error, "ERROR:void value not ignored as it ought to be"

(choice=='Y'||choice=='y')?((printf("\n...The program is now resetting...\n")&&(main()))):((printf("\n\tThank you for trying out the BPCC App.\n"))&&(exit(0))); 

如果我删除 exit(0) 整个程序运行正常,但我需要包括 exit(0)

你能帮帮我吗?

来自 C11 标准,第 6.5.13 章,逻辑与 [&&] 运算符

Each of the operands shall have scalar type.

和来自exit()man page

void exit(int status);

现在,void 不是有效值(或标量类型)。所以,你不能写

...:((printf("\n\tThank you for trying out the BPCC App.\n"))&&(exit(0)));
                                                               |--------|
                                        The return value of `exit()` is used
                                            as one of the operands of `&&` here

因此出现错误。您不能使用 exit()return 值 来编写逻辑(基本上有什么意义?)。你必须想出一些替代方案。 (就像调用 exit() 作为下一条指令或类似指令)。正如 @BLUEPIXY 先生在 中提到的,一种可能的方法是使用 comma 运算符 [,],如下所示

.... (printf("\n\tThank you for trying out the BPCC App.\n")) , (exit(0))

也就是说,这种方法(递归调用 main())不是一个好的做法。

试试这个

(printf("message") && (exit(0),1))

(printf("message") , exit(0))

函数exit声明如下

_Noreturn void exit(int status);

所以你不能在这样的表达式中使用它

(printf("\n\tThank you for trying out the BPCC App.\n"))&&(exit(0))

您可以使用逗号运算符代替运算符 &&。

因此三元运算符可以如下所示

( choice == 'Y' || choice == 'y' ) 
   ? ( printf( "\n...The program is now resetting...\n" ) , ( void )main() )
   : ( printf( "\n\tThank you for trying out the BPCC App.\n" ), exit( 0 ) ); 

或以下方式

( choice == 'Y' || choice == 'y' ) 
   ? ( printf( "\n...The program is now resetting...\n" ) , exit( main() ) )
   : ( printf( "\n\tThank you for trying out the BPCC App.\n" ), exit( 0 ) );