Files
BlogPosts/YueQian/Homework/1.23.md

76 lines
2.2 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

---
tags:
- "#作业"
- C语言
aliases: empty
日期: 2026/1/23
---
1.定义`int num = 255`,分别以十进制(有符号)、无符号十进制、八进制(带 / 不带前缀)、十六进制(小写带 / 不带前缀、大写带 / 不带前缀) 格式输出;
```c
#include <stdio.h>
int main(int argc, char const *argv[])
{
    int num = 255;
    printf("%d\n",num);
    printf("%u\n",num);
    printf("%#o\n",num);
    printf("%o\n",num);
    printf("%#x\n",num);
    printf("%x\n",num);
    printf("%#X\n",num);
    printf("%X\n",num);
    return 0;
}
```
2.定义`long num_long = 123456789``long long num_ll = 9876543210`,分别用对应控制符输出;
```c
#include <stdio.h>
int main(int argc, char const *argv[])
{
    long num_long = 123456789;
    long long num_ll = 9876543210;
    printf("%ld\n",num_long);
    printf("%lld\n",num_ll);
    return 0;
}
```
3.定义`float pi = 3.1415926`,分别以默认小数形式、保留 2 位小数、指数形式(保留 3 位精度) 输出;
```c
#include <stdio.h>
int main(int argc, char const *argv[])
{
    float pi = 3.1415926;
    printf("%f\n",pi);
    printf("%.2f\n",pi);
    printf("%.3e\n",pi);
    return 0;
}
```
4.定义字符串`char *name = "Programmer"`,分别输出完整字符串、前 5 个字符、左对齐占 10 位、右对齐占 10 位的格式;
```c
```
5.编写一个综合程序结合格式化输入输出、类型转换、IO 流知识点,实现 “字符↔ASCII 码” 双向转换:
1. 提示用户选择功能:输入 1字符转 ASCII、2ASCII 转字符);
2. 若选择 1接收用户输入的单个字符注意需处理 scanf 接收字符时的缓冲区问题),输出该字符的 ASCII 码结合类型转换char→int
3. 若选择 2接收用户输入的 ASCII 码值0-127输出对应的字符int→char显式转换
4. 增加输入校验:若输入的 ASCII 码超出 0-127 范围,输出 “无效的 ASCII 码”;若输入的不是单个字符,输出 “输入格式错误”;
5. 核心要求使用scanf的返回值判断输入是否有效结合格式化控制符完成输入输出