【发布时间】:2021-11-20 16:31:23
【问题描述】:
我想使用以下代码获取用户输入:
uint32_t value;
printf("value: ");
scanf("%"SCNu32, &value);
我现在的问题是,我将如何使用用户输入(在我的情况下为值),然后将其格式化为二进制数,而函数 print_binary 中没有循环?输出必须是 0b 之后的 32 位。我不能在任何地方使用任何类型的循环。
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
void print_binary(uint32_t value){
printf("%d = 0b",value);
//I want to print the variable value with a fixed length of 32 and that it is
//binary with the starting index of 0bBinaryValueOfNumber.
return;
}
int main(void) {
uint32_t value;
printf("value: ");
if (scanf("%"SCNu32, &value) != 1) {
fprintf(stderr, "ERROR: While reading the 'uint32_t' value an error occurred!");
return EXIT_FAILURE;
}
printf("\n");
print_binary(value);
printf("\n");
return EXIT_SUCCESS;
}
我也有以下例子:
如果用户输入为 5,则函数应返回“5 = 0b00000000000000000000000000000101”。
【问题讨论】:
-
感谢您的快速解决方案,不幸的是,我不允许使用 for 循环或 while 循环。还有其他可能吗?
-
sry 现在做了编辑,我的错。
标签: c formatting