繁体   English   中英

在三元运算符中使用printf时,else条件不会打印任何内容。为什么?

[英]The else condition does print anything when printf is used in ternary operator.. why?

三元运算符中的另一个部分(即printf语句)在代码中不起作用,语法正确吗?还是有些愚蠢的错误?

#include<stdio.h>
#define isNegative(x) x<0 ? 1 : 0
#define isPositive(x) isNegative(x) ? 0 : 1
#define isEven(x) x%2 ? 0 : 1
#define isOdd(x) isEven(x) ? 0 : 1
main(){
 int n,ch;
do{
printf("Enter a number\n");
scanf("%d",&n);
printf("Choose an operation :\n 1.isEven\n 2.isOdd\n 3.isPositive\n 4.isNegative\n");
scanf("%d",&ch);
switch(ch){
case 1 :isEven(n) ? printf("Its even number\n") : printf("Its not an even number\n") ;
        break;
case 2 :isOdd(n) ? printf("Its odd number\n") : printf("Its not an odd number\n") ;
        break;
case 3 :isPositive(n) ? printf("Its a positive number\n") : printf("Its not a positive number\n");
        break;
case 4 :isNegative(n) ? printf("Its a negative number\n") : printf("Its not a negative number\n");
        break;
default : printf("Enter valid option\n");
        break;
}
printf("Press 5 to continue or 6 to exit\n");
scanf("%d",&ch);
}while(ch!=6);
}

代码的逻辑正确吗? 头文件的内容

三元运算符不同于if - then - else语句; 它本身就是一个表达式,这意味着它的是按常规方式求的任何 您无法在代码中看到它,因为您使用的三元运算符表达式的结果不会被任何人捕获。 在C ++中,这就是所谓的rvalue

您实际上可以通过执行以下操作向自己证明这一点:

size_t i = 1;    
(i == 2) ? printf("hello\n") : printf("goodbye\n")

输出:

goodbye

再次在这里,我们只是评估谓词并评估 “其他”表达式。 毫不奇怪,输出结果是goodbye 这还不是全部。

许多人没有意识到printf具有返回值。 它将返回写入stdout的字符数,或者将fprintf给指定的任何输出流。 类似于scanf函数, printf会在错误时返回负数。 这就是为什么检查scanf的返回值很重要的原因。 不幸的是,许多人没有意识到这些函数具有返回值,因此显然他们不检查自己不知道的内容。

不过,通过下面的示例,您可以清楚地看到三元表达式的返回值。

size_t i = 1;    
int result = (i == 2) ? printf("hello\n") : printf("goodbye\n");
printf("Result: %d\n", result);

输出:

goodbye
Result: 8

回到实际的三元运算符,我想强调一下这是一个表达式 您会在函数式编程语言中经常看到这样的事情,尽管您可能会看到不同的“形状”。 printf函数正在打印到stdout的事实是所谓的副作用,而在仍具有有用的编程语言的同时尽可能减少副作用的概念是函数式编程的基础之一。


要回答您的特定问题:

代码的逻辑正确吗?

我重新格式化了一些代码,并添加了许多括号。 当您同时使用宏和三元运算符时,必须非常小心,以使括号正确,否则会出现类似的错误。

#include <stdio.h>

#define isNegative(x) x<0 ? 1 : 0
#define isPositive(x) isNegative(x) ? 0 : 1
#define isEven(x) x%2 ? 0 : 1
#define isOdd(x) isEven(x) ? 0 : 1

int main()
{
    int n, ch;

    do {
        printf("Enter a number\n");
        scanf("%d", &n);

        printf("Choose an operation :\n 1.isEven\n 2.isOdd\n 3.isPositive\n 4.isNegative\n");
        scanf("%d", &ch);

        switch (ch) {
            case (1): ((isEven(n)) ? printf("Its even number\n") : printf("Its not an even number\n"));
                break;
            case (2): ((isOdd(n)) ? printf("Its odd number\n") : printf("Its not an odd number\n"));
                break;
            case (3): ((isPositive(n)) ? printf("Its a positive number\n") : printf("Its not a positive number\n"));
                break;
            case (4): ((isNegative(n)) ? printf("Its a negative number\n") : printf("Its not a negative number\n"));
                break;
            default: printf("Enter valid option\n");
                break;
        }

        printf("Press 5 to continue or 6 to exit\n");
        scanf("%d",&ch);
    } while(ch != 6);

    return EXIT_SUCCESS;
}

执行:

Enter a number
5
Choose an operation :
 1.isEven
 2.isOdd
 3.isPositive
 4.isNegative
4
Its not a negative number
Press 5 to continue or 6 to exit
5
Enter a number
3
Choose an operation :
 1.isEven
 2.isOdd
 3.isPositive
 4.isNegative
3
Its a positive number
Press 5 to continue or 6 to exit

希望这会有所帮助,伙计,祝你好运。 如果您有任何疑问,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM