qfedu-c-level/day5/homework/h1.c

27 lines
625 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 请编程定义char变量并赋值\101, 输出这个变量的十进制数和字符内容。
// 【提示】\ddd 代表8进制 % c 输出字符
#include <stdio.h>
void printBinary(char num)
{
int size = sizeof(num);
int i = size * 8 - 1;
while (i--)
{
int bit = (num >> i) & 1;
printf("%d", bit);
}
printf("\n");
}
int main()
{
char x = '\101';
printf("十进制数: %d\n", x);
printf("二进制数: ");
printBinary(x);
printf("八进制数: %o\n", x);
printf("十六进制数: 0x%hhx\n", x);
printf("字符内容: %c\n", x);
return 0;
}