[C++][参考]字符串格式化控制符

卫衣 / 双肩包 / T 恤/羽绒服等任选,下单送 CSDN 年卡 程序员周边任选一款实物,直接赠送 CSDN 会员年卡+ Coding Plan,写代码学习装备一起拿下 阅读详情

The format-control string contains format specifications that determine the output format for the arguments following the lpFmt parameter. Format specifications, discussed below, always begin with a percent sign (%). If a percent sign is followed by a character that has no meaning as a format field, the character is not formatted (for example, %% produces a single percent-sign character).

The format-control string is read from left to right. When the first format specification (if any) is encountered, it causes the value of the first argument after the format-control string to be converted and copied to the output buffer according to the format specification. The second format specification causes the second argument to be converted and copied, and so on. If there are more arguments than format specifications, the extra arguments are ignored. If there are not enough arguments for all of the format specifications, the results are undefined.

A format specification has the following form:

%[-][#][0][width][.precision]type

Each field is a single character or a number signifying a particular format option. The type characters that appear after the last optional format field determine whether the associated argument is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). The optional fields control other aspects of the formatting. Following are the optional and required fields and their meanings.

FieldMeaning
Pad the output with blanks or zeros to the right to fill the field width, justifying output to the left. If this field is omitted, the output is padded to the left, justifying it to the right.
#Prefix hexadecimal values with 0x (lowercase) or 0X (uppercase).
0Pad the output value with zeros to fill the field width. If this field is omitted, the output value is padded with blank spaces.
widthCopy the specified minimum number of characters to the output buffer. The width field is a nonnegative integer. The width specification never causes a value to be truncated; if the number of characters in the output value is greater than the specified width, or if the width field is not present, all characters of the value are printed, subject to the precision specification.
.precisionFor numbers, copy the specified minimum number of digits to the output buffer. If the number of digits in the argument is less than the specified precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds the specified precision. If the specified precision is 0 or omitted entirely, or if the period (.) appears without a number following it, the precision is set to 1.
 For strings, copy the specified maximum number of characters to the output buffer.
typeOutput the corresponding argument as a character, a string, or a number. This field can be any of the following values.
 ValueMeaning
 cSingle character. This value is interpreted as type WCHAR if the calling application defines UNICODE and as type CHAR otherwise.
 CSingle character. This value is interpreted as type CHAR if the calling application defines UNICODE and as type WCHAR otherwise.
 dSigned decimal integer. This value is equivalent to i.
 hc, hCSingle character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type CHAR, even when the calling application defines UNICODE.
 hdSigned short integer argument.
 hs, hSString. This value is always interpreted as type LPSTR, even when the calling application defines UNICODE.
 huUnsigned short integer.
 iSigned decimal integer. This value is equivalent to d.
 lc, lCSingle character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type WCHAR, even when the calling application does not define UNICODE.
 ldLong signed integer. This value is equivalent to li.
 liLong signed integer. This value is equivalent to ld.
 ls, lSString. This value is always interpreted as type LPWSTR, even when the calling application does not define UNICODE. This value is equivalent to ws.
 luLong unsigned integer.
 lx, lXLong unsigned hexadecimal integer in lowercase or uppercase.
 pWindows 2000/XP: Pointer. The address is printed using hexadecimal.
 sString. This value is interpreted as type LPWSTR when the calling application defines UNICODE and as type LPSTR otherwise.
 SString. This value is interpreted as type LPSTR when the calling application defines UNICODE and as type LPWSTR otherwise.
 uUnsigned integer argument.
 x, XUnsigned hexadecimal integer in lowercase or uppercase.

 

C++中的部分常用的格式化输出的分析 C++为标准输入和输出定义了一些格式标志, 它可以通过flags(), setf(), 和 unsetf() 三个函数来控制. (1)cout.setf()的作用是通过设置格式标志来控制输出形式 (2)cout.flags()的用法可以参考setiosflags的用法进行操作(暂时未在一般的搜索引擎上搜索到关于该函数的详细用法) (3)cout.unsetf()的作用是取消已设的那个格式定义 我个... 阅读详情

相关推荐

关于C++的cout的格式化输出

今天呢,有人问我关于一个C++浮点数输出精度的问题,毕竟到现在我也还没怎么去考量那些操作符,也就一时没答上来,查了一下,回答了那个问题,也顺便做个整理,整体的关于那些输出的manipulator。 首先是那个不太对的代码 #include #include using namespace std; int main(){ const double PI = 3.14; const

ReaNAiveD的博客 625

the format control string

The format-control string contains format specifications that determine the output format for the arguments following the lpFmt parameter. Format specifications, discussed below, always begin with a p

eulb的专栏 1701

c++,格式控制

c++,格式控制,不小于5个汉字或10个字母,详细的标题容易被下载。

C/C++输出格式与输出控制符

C语言输出格式控制符大全 C++输出保留指定位小数(输出精度控制) C++输出格式控制符

Iamme 6474

c++中如何用string实现CString格式化的功能

c++中如何用string实现CString格式化的功能 内容目录: 也就是实现类似下面的功能: CString str; double d = 10.24; str.Format("d=%.2f",10.24); 其实用c++标准库来实现也比较简单,代码如下: #include using namespace std;   .. ostringstre

haiross的专栏 9574

深入剖析C++中的string

comments:本文摘自于网络 一,C语言的字符串 在C语言里,对字符串的处理一项都是一件比较痛苦的事情,因为通常在实现字符串的操作的时候都会用到最不容易驾驭的类型——指针。 比如下面这个例子: //example 1: char str[12] = "Hello"; char *p = str; *p = ''h''; //改变第一个字母 //example 2:...

weixin_34194379的博客 2747

字符串格式控制(C++

输入一个字符串str,满足以下三个条件,返回True: 1、字符只能是 大小写字母 数字 2、首字符必须是大写字母 3、搭配组合必须满足三个中的两个:(1)大写字母+数字;(2)小写字母+数字;(3)大小写字母+数字 ...

Dream19961996的博客 1169

【现代C++】新的字符串格式化方法

(点击上方公众号,可快速关注)本篇文章主要介绍现代C++字符串格式化的方法。在此之前,回顾了一些老的字符串格式化的方法,并分析各自的优劣。在最后给出了一种提供给老编译器的折中方案,因为新的...

1万+

【C】C语言printf,格式化字符串,缓冲区

C语言格式化字符串 文章目录C语言格式化字符串前言格式化字符串printf 常用的format标签:利用C11特性 _Generic 写的泛型print相关库函数其他问题%x、%X、%#x、%#X 的区别关于`%I64d` 和 `%lld`如何输出 %d、\ 和双引号 前言 用惯了 Python 字符串的 format,感觉 C 的 printf 用起来好别扭,于是写这篇来帮忙记忆一些细节。 格...

(ノ゚▽゚)ノ的博客 3483

C++标准IO库】字符串

C++ 标准 IO 库中的字符串流(istringstream、ostringstream 和 stringstream)为我们提供了强大而灵活的字符串处理功能。通过字符串流,可以方便地实现字符串与其他数据类型之间的转换、字符串的分割和拼接、数据的解析和格式化等操作。在使用字符串流时,需要注意错误处理和性能考虑,以确保程序的健壮性和高效性。

byte轻骑兵的技术小窝 5619

C++字符串std::string

https://blog.csdn.net/SavitarYu/article/details/81029676 https://blog.csdn.net/qq_38877888/article/details/104010047

qq_43148810的博客 1471

C语言转义字符和格式控制符参考

转义字符参考: \a:蜂鸣,响铃 \b:回退:向后退一格 \f:换页 \n:换行,光标到下行行首 \r:回车,光标到本行行首 \t:水平制表 \v:垂直制表 \\:反斜杠 \':单引号 \":双引号 \?:问号 \ddd:三位八进制 \xhh:二位十六进制 \0:空字符(NULL),什么都不做 注: 1

zhangzhi123456789的专栏 2383

c语言缓冲区字符串,【C】C语言printf,格式化字符串,缓冲区

C语言printf,格式化字符串,缓冲区文章目录C语言printf,格式化字符串,缓冲区前言格式化字符串printf 常用的format标签:其他format指示符`%s``%p``%a``%n`子格式控制符相关库函数printf 缓冲区(缓存区)问题fprintf函数,stdout,stderr,缓冲区什么是缓冲区为什么需要缓冲区缓冲区类型缓冲区的大小缓冲区什么时候会被刷新(清空)getchar...

weixin_39785081的博客 393

C++专题】格式化输出与输入

本文介绍了C/C++中的格式化输入输出函数printf和scanf的使用方法。主要内容包括: printf的基本语法、常用格式说明符(%d、%f、%c等)及其扩展用法(控制小数位数、输出宽度和对齐方式) scanf的基本语法和常用格式说明符,强调变量前必须加取地址符& 提供了4个典型例题及参考代码,演示如何实现字符与ASCII码转换、格式化输出不同类型数据以及从固定格式字符串中提取数据 文章重点讲解了格式化输入输出的核心语法和常见应用场景,适合初学者掌握基础输入输出操作。

Dust的博客 534

C++ 的 format 和 vformat 函数

介绍 C++ 20 引入的 format 和 vformat 函数,以及 C++ 23 和 26 的持续改进内容

oRbIt 的专栏 2691

LuaForUnity5:Lua的字符串

一、字符串处理: string.upper():转大写 string.lower():转小写 string.reverse():字符串反转 string.len():获取长度 string.gsub():字符串替换 string.sub():字符串截取 string.find():寻找子串 详细使用具体看代码,有注释 print("---------------------...

加载中... 591

C++ 20新特性之std::format

C++ 20之前,我们通常使用printf、stringstream、cout等流对象,并结合各种流操作符(比如:

hope_wisdom的技术博客 1723

ssprintf 和 sscanf 以及一些其他的字符串处理的函数

1、ssprintf、sscanf用头文件 <cstdio> ssprintf: sprintf的作用是将一个格式化字符串输出到一个目的字符串中,而printf是将一个格式化字符串输出到屏幕。sprintf的第一个参数应该是目的字符串,如果不指定这个参数,执行过程中出现 "该程序产生非法操作,即将被关闭...."的提示。 格式: ssprintf(字符串,“格式控制符”,数据); sscanf: sscanf()会将参数str 的字符串根据参数format(格式化字符串)..

qq_58333162的博客 926

C++中的《数字字符串类型-数字类型转换总结》

C++11中,由于右值引用的引入,常为人诟病std::string的性能问题得到了很大的改善。另一方面,我们也可以看到新语言为std::string类增加了很多新的api。比较引人注意的有std::string的成员函数stoi系列,以及std::to_string全局函数。这两种API虽很不起眼,却为C++11的格式化输出(formatted I/O)增加了一种实用的手段。我们可以依序会议一下C...

PzLu's Blog 785
上一篇: [DirectX][DirectInput]Summarize:Using the Keyboard
下一篇: [MFC]SetTimer()设置多个定时器
hicoder
博客等级 码龄16年 0粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值