当前位置:网站首页>错题汇总1
错题汇总1
2022-04-23 09:04:00 【云雷屯176】
1.以下程序的运行结果是()
int main(void) {
printf("%s , %5.3s\n","computer","computer");
return 0;
}
A computer , puter
B computer , com
C computer , computer
D computer , compu.ter
解析:题示代码中的%5.3s ,其具体的格式应该是:
%m.ns
其中,m表示输出字符串的宽度,如果字符串不够,则在左侧补空格
n表示左起截取目标字符串的n个字符,如果字符串长度小于n,就输出原字符串
int main() {
printf("123456789abcdef\n");
printf("%15s", "comptuer");
printf("END\n");
printf("%1s", "comptuer");
printf("END\n");
printf("%.4s", "comptuer");
printf("END\n");
printf("%.15s", "comptuer");
printf("END\n");
printf("%15.4s", "comptuer");
printf("END\n");
printf("%15.15s", "comptuer");
printf("END\n");
return 0;
}
我们可以通过以上结果进行一些简单验证。
2.使用printf函数打印一个double类型的数据,要求:输出为10进制,输出左对齐30个字符,4位精度。以下哪个选项是正确的?
A %-30.4e
B %4.30e
C %-30.4f
D %-4.30f
解析:这道题算是上一道题的一个补充:
%m.ns中m关于左右对齐的设定
当m大于0的时候,为右对齐,m小于0的时候,为左对齐。
int main() {
printf("123456789abcdef\n");
printf("%-15s", "comptuer");
printf("END\n");
printf("%15s", "comptuer");
printf("END\n");
return 0;
}
说简单点就是m>0,且m大于字符串长度时,在字符串左边补空格,m<0,且m大于字符串长度时,在字符串右边补空格。
另外,补充一个小知识:
%d 整型输出,%ld长整型输出。
%o 以八进制数形式输出整数。
%x 以十六进制数形式输出整数。
%u 以十进制数输出unsigned型数据(无符号数)。
%c 用来输出一个字符。
%s 用来输出一个字符串。
%f 用来输出实数,以小数形式输出。
%e 以指数形式输出实数。
%g 根据大小自动选f格式或e格式,且不输出无意义的零。
//举几个简单的小例子
int main() {
printf("%e\n", 13.2);
printf("%g\n", 13.2);
printf("%g\n", 0.00000043);
return 0;
}
3.下面程序输出是什么?
#include <stdio.h>
int main()
{
int a=1,b=2,c=3,d=0;
if(a == 1 && b++==2)
if(b!=2||c--!=3)
printf("%d,%d,%d\n" ,a,b,c);
else
printf("%d,%d,%d\n" ,a,b,c);
else
printf("%d,%d,%d\n" ,a,b,c);
return 0;
}
A 1,2,3
B 1,3,2
C 3,2,1
D 1,3,3
解析:这道题的重点就在于&&与||的短路原则,之前往往碰到的是&&的短路,反倒把||的短路给忘了,错的着实不该。
&&的短路原则:
若要使(表达式1)&&(表达式2)为真,就要使表达式1与表达式2都为真,但如果表达式1为假,编译器会跳过对表达式2的判断
||的短路原则:
若要使(表达式1)||(表达式2)为真,则需要表达式1或者表达式2为真,所以当表达式1为真时,编译器会跳过表达式2.
4.设变量已正确定义,以下不能统计出一行中输入字符个数(不包含回车符)的程序段是
A n=0;while(ch=getchar()!='\n')n++;
B n=0;while(getchar()!='\n')n++;
C for(n=0;getchar()!='\n';n++);
D n=0;for(ch=getchar();ch!='\n';n++);
解析:咳咳......全体目光向我看齐,我是个@#
for循环的初始化部分只执行一次。
5.假设在一个 32 位 little endian 的机器上运行下面的程序,结果是多少?
#include <stdio.h>
int main(){
long long a = 1, b = 2, c = 3;
printf("%d %d %d\n", a, b, c);
return 0;
}
A 1,2,3
B 1,0,2
C 1,3,2
D 3,2,1
解析:这道题所考察的是计算机大小端存储相关的知识:
大端存储:低位存在高地址
小端存储:低位存在低地址
由于做题的时候太马虎,以至于没有看出long long型与%d之间的坑。
long类型可能在不同的操作系统下,不同的编译器下长度可能不同,但long long类型却是公认的占用8个字节的类型。
题中给出了操作系统是32位little endian(小端)操作系统,所以 1 在该机器上的存储应该是:
0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
(好长啊......)低地址-->高地址
而%d是只读取4个字节(按照int类型读取的,long long 应该用%ld)。
由以上条件,易得,答案为1,0,2
最后,是我个人感觉需要提醒自己一下的一个零碎知识点。
在C语言中,多维数组初始化时,只有第一维可以省略。
版权声明
本文为[云雷屯176]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_56713382/article/details/124350491
边栏推荐
- About CIN, scanf and getline, getchar, CIN Mixed use of getline
- Get trustedinstaller permission
- Share the office and improve the settled experience
- L2-022 重排链表 (25 分)(map+结构体模拟)
- Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
- 共享办公室,提升入驻体验
- [58] length of the last word [leetcode]
- Complete binary search tree (30 points)
- GUI编程简介 swing
- Introduction to GUI programming swing
猜你喜欢

Strength comparison vulnerability of PHP based on hash algorithm

1099 establish binary search tree (30 points)

2021 Li Hongyi's adaptive learning rate of machine learning

在sqli-liabs学习SQL注入之旅(第十一关~第二十关)

Correct method of calculating inference time of neural network

node安装

L2-022 rearrange linked list (25 points) (map + structure simulation)

Arbre de dépendance de l'emballage des ressources

LeetCode_DFS_中等_1254. 统计封闭岛屿的数目

I don't understand time, timestamp and time zone. Look at this article
随机推荐
Failed to download esp32 program, prompting timeout
小程序报错:Cannot read property 'currentTarget' of undefined
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
深度学习框架中的自动微分及高阶导数
Share the office and improve the settled experience
On array replication
搜索树判断 (25 分)
GUI编程简介 swing
Notes d'apprentissage oneflow: de functor à opexprinterpreter
Is Zhongyan futures safe and reliable?
Single chip microcomputer nixie tube stopwatch
L2-3 romantic silhouette (25 points)
是否完全二叉搜索树 (30 分)
L2-023 graph coloring problem (25 points) (graph traversal)
Complete binary search tree (30 points)
PCTP考试经验分享
The K neighbors of each sample are obtained by packet switching
Go language self-study series | golang method
[C language] document operation
LaTeX数学公式