簡體   English   中英

當變量類型不同時,為什么下面的代碼給出不同的輸出?

[英]Why the below code is giving different output when variable type is different?

//o/p when i/p is 16 and 2 is 4 and if  variable is int then o/p will be 20;   

#define SETBIT(A,B) A|1<<B

int main(){
    char n,pos;
    printf("Enter a value");
    scanf("%d",&n);
    printf("Enter position");
    scanf("%d",&pos);
    printf("Value after setting %d",SETBIT(n,pos));
}

對於*scanf函數, d轉換說明符期望其對應的參數具有int *類型; 如果不是這種情況,則行為是undefined ,幾乎任何結果都是可能的。

如果要對posn使用char ,則必須在scanf調用中使用%hhd而不是%d

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM