【发布时间】:2016-09-21 16:30:30
【问题描述】:
//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",&n);格式说明符与数据类型不匹配,其他scanf也是。 -
scanf("%d",&n)和char n会产生未定义的行为。scanf("%d",&pos)也一样。在实践中,scanf将 4 字节的数据写入大小为 1 字节的变量中(尽管这些大小通常不受语言标准的规定,但在大多数平台上都是如此)。您需要确定要从键盘扫描的数据类型。使用"%d"- 使用int。对于char- 使用"%c"。 -
请提交格式化代码
-
@WeatherVane:我想你的意思是
((A)|1<<(B)) -
@WeatherVane 所有宏参数都应在宏中用括号括起来。
SETBIT(1, 2|4)的结果可能与SETBIT(1, 6)不同