簡體   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