我在尝试使用inttypes.h时编写的简单程序:
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
bool get_bit(uint32_t x, uint8_t n) {
x >>= n;
return x & 1;
}
int main() {
uint32_t x;
uint8_t n;
printf ("Enter x: ");
scanf("%"SCNu32, &x);
printf ("Enter n: ");
scanf("%"SCNu8, &n);
printf("The %"PRIu8"th bit of %"PRIu32" is: %d", n, x, get_bit(x, n));
return 0;
}
在我的手机上(64位八核ARN LTE Soc Android 10),它可以很好地工作:
Enter x: 1
Enter n: 0
The 0th bit of 1 is: 1
但是在我的电脑上(64位x86 Windows 10),我得到以下结果:
Enter x: 1
Enter n: 0
The 0th bit of 0 is: 0
将bool更改为uint8_t不会影响它。
编辑:我尝试使用MinGW-w64 GCC C99和C17进行编译。
scanf
函数的返回值。 - user694733