当前位置:网站首页>[code shoe set novice village question 600] for the formatting control of strings, that is, the width and accuracy of strings
[code shoe set novice village question 600] for the formatting control of strings, that is, the width and accuracy of strings
2022-07-19 13:38:00 【Sandm *】
Topic link :
Their thinking :
For non character data, we have %nd %m.nf
For strings, we also have their width and precision ,
- namely %m.ns
Among them m It represents the width , That is, how many columns
n Then the first few digits of the string are used
- Be careful :
%m.ns To work with %m.nf( there n It means precision ) Make a distinction
It's easy to get wrong :
because scanf When a function encounters a space when entering , It will automatically stop reading data after spaces
- So an examination site of this topic :
How to read a whole line of string in a string containing spaces ?
have access to gets ( ) function ( This function is only used to read strings ),
This function ends the reading with the Enter key , Therefore, the input will not be terminated when encountering spaces
- But this function has a disadvantage :
When receiving the input of string ,
It does not check the array subscript ,
So if the input character length exceeds the maximum space that the character array can hold , No errors at compile time ,
But when running, the array will be out of bounds , Or memory leaks
- And to solve these problems ,
C++ There's one in it cin.getline ( ) function , It limits the length of the character array in which the string is stored
Reference code :
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char arr[6] = {'0'};
/* string The type is not C The built-in type of language , So it can't be used for printf function */
//scanf("%6s",arr); Stop reading when encountering spaces
gets(arr);
printf("%6.3s",arr);
return 0;
}边栏推荐
- Use golang to correctly process the IP data of the five major Internet registration agencies
- Acwing game 60
- [Yugong series] July 2022 go teaching course 012 forced type conversion
- How to upgrade Flink job gracefully?
- 【码蹄集新手村 600 题】科学计数法的实现方式,输出指数形式
- Deep learning from getting started to giving up the 100 day challenge
- 【6.15】Codeforces Round #798 (Div. 2)
- 【码蹄集新手村 600 题】计算一个整数有多少位数
- Codeforces Round #808 (Div. 2)ABCD
- Flutter 使用 AnimatedSwitcher 做场景切换
猜你喜欢

Interviewer: is it acceptable to transfer to go?

torch. utils. data. Dataloader description

LeetCode 0117. Populate the next right node pointer II of each node

Onvif protocol related: 4.1.1 WS username token method to obtain wsusernametokenbean
![[postgraduate entrance examination vocabulary training camp] day 6 - eventually, state, create, productivity, stimulate](/img/61/a8a366ae3d058be01f9408f9f26cac.png)
[postgraduate entrance examination vocabulary training camp] day 6 - eventually, state, create, productivity, stimulate
![[pyGame learning notes] 6 Cursor mouse cursor](/img/ea/70fb3043ae32eca68e31144a54b651.jpg)
[pyGame learning notes] 6 Cursor mouse cursor

codeforce:A. Doremy‘s IQ【反向贪心】

Responsive dream weaving template wine cellar website

【7.13】代码源 -【饿饿 饭饭】【路径计数2】【函数求和】

【码蹄集新手村 600 题】计算一个整数有多少位数
随机推荐
565.数组嵌套
Array simulation queue
OpenSSL操作
Flutter uses animatedswitcher to switch scenes
Google Earth Engine——1992—至今混合坐标海洋模型、水温和盐度(全球海洋数据集HYCOM)
【码蹄集新手村 600 题】运算符 / 在不同的运算顺序中的类型转换
[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
565. Array nesting
【码蹄集新手村 600 题】输出时的左对齐,右对齐
onvif协议相关:3.1.3 Digest方式获取截图url
onvif协议相关:4.1.1 WS-Username token方式获取WSUsernameTokenBean
【CANN训练营】昇腾AI基础知识介绍
模块7(王者荣耀商城异地多活架构设计)
torch.utils.data.DataLoader说明
名片管理的框架搭建
Simulation of overvoltage protection (OVP) circuit based on PMOS
【6.15】Codeforces Round #798 (Div. 2)
onvif协议相关:2.1.1 none方式获取token
【南瓜书ML】(task2)线性模型的数学推导(最小二乘估计、广义瑞利商、极大似然估计等)
模板虚拟机环境准备