繁体   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