![](/img/trans.png)
[英]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.