[英]3 different functions giving the same output depending on variable type in C
[英]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 ,幾乎任何結果都是可能的。
如果要對pos
和n
使用char
,則必須在scanf
調用中使用%hhd
而不是%d
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.