C++中C/C++格式化输出
对于不同的机器,一此格式化输出的函数经常会得不到正确的输出,比方小端上的程序在大端上执行等,另外,在日常程序开发时,也会经常被这种小问题而困扰非常久。终于发现是她的问题。不免有点叹息,以下对printf/wprintf/CString.Format等相关函数所使用的格式化输出的语法作一总结。有些是从官方资料摘过来的,还是英文的。
函数中格式化声明的一般格式为:
%[flags] [width] [.precision] [{h | l | ll | I | I32 | I64}]type
当中的每个蓝色项都代表了格式化输出格式中的一个选项。
这些输出格式选项一般使用一个字母或者一个数字来表示特定的格式。该格式说明语句最简单的形式就是百分号%加上一个类型说明符type。比如%s。假设%后跟的字符并不是type类型的选项之中的一个。那么这个字符将以文本的形式输出。
比如在输出百分号时所採用的语句%%。
出如今type之前的其它选项都分别控制了格式化输出的其它方面,详细例如以下:
- type
- 该选项须要一个字符来确认要输出的參数是以字符的形式输出,还是以字符串的形式输出,或者仅仅是输出一个数字;具体參见本文附录1。
- flags
- 它由一个或者若干个字符组成。它控制了输出的对齐方式、正负号、小数点、空白字符和十进制或者是十六进制的前缀等。
多个flag可同一时候出如今同一输出格式控制语句中。
- width
- 一个数字;用来说明输出的最小位数。
- precision
- 一个数字;用来说明所有输出字符的或者部分输出字符的最大位数。对于整型。该项限定了输出数字的最少个数。
- h| l| ll| I| I32| I64
- 用来表示输出參数size的前缀(long/short, 32-/64-bit, single-byte字符/宽字符)。
具体參见本文附录2。
- 一种良好的编程习惯:
- 在编敲代码的时候。不要在程序中出现类似于printf(name)的语句。
尽管该语句在C语言中输出name这个字符串,可是实际上这是非常危急的。假设name字符串中包括了“%s”,那么该语句将会在执行时失败。应该将该语句写为printf("%s",name);这种语句才是安全的。
注意:类型说明符 C, n, p和S的行为, 还有c和s在printf函数中的行为,都是Microsoft的扩展,与ANSI标准不兼容。
|
character |
Type |
Output format |
|---|---|---|
|
c |
int orwint_t |
用在printf函数中,表示的是一个single-byte字符。而用在wprintf函数中,表示的是一个宽字符。 |
|
C |
int orwint_t |
用在wprintf函数中,表示的是一个single-byte字符。而用在printf函数中,表示的是一个宽字符。(与c刚好相反) |
|
d |
int |
有符号十进制整数 |
|
i |
int |
有符号十进制整数 |
|
o |
int |
八进制无符号整数 |
|
u |
int |
无符号十进制整数 |
|
x |
int |
无符号十六进制整数,使用"abcdef"标识。 |
|
X |
int |
无符号十六进制整数。使用"ABCDEF"标识。 |
|
e |
double |
有符号值,输出形式为[ – ]d.dddd e [sign]dd[d]。 当中d是一个十进制的数字,其范围为0-9;dddd是一个或者多个十进制的数字。dd[d]是两个或者三个十进制的数字。其值依赖于输出的格式和指数的大小。 符号是+、-。 |
|
E |
double |
和e相似。仅仅是将输出中的e用E取代。 |
|
f |
double |
有符号值。输出形式为[-]dddd.dddd。小数点前面的数字由输出值的大小决定。而小数点后面的数字由输出的精度决定。 |
|
g |
double |
Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less |
|
G |
double |
Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). |
|
a |
double |
有符号双精度十六进制浮点数。输出形式为[−]0xh.hhhh p±dd。precision选项将决定小数点后面的位数。(using lower case letters) |
|
A |
double |
有符号双精度十六进制浮点数。 输出形式为[−]0xh.hhhh P±dd。precision选项将决定小数点后面的位数。(using capital letters) |
|
n |
Pointer to integer |
Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. See Security Note below. |
|
p |
Pointer to void |
以十六进制的形式输出地址 |
|
s |
String |
当和printf函数使用的时候。输出的是single-byte字符串。 而和wprintf使用的时候输出的是宽字符串;会依次输出全部字符知道遇到第一个null字符或者由precision值限定; |
|
S |
String |
当和wprintf函数使用的时候。输出的是single-byte字符串。而和printf使用的时候输出的是宽字符串;会依次输出全部字符知道遇到第一个null字符或者由precision值限定; |
Note:假设和%s或者%S相关的參数是一个null指针。将会输出“(null)”.
Note:在全部的指数格式中,默认的指数位数为3位;能够利用函数_set_output_format设定输出的位数为2。然后输出会依据须要扩展到3位。
Security Note: %n格式符是不安全的,在默认情况下是被禁用的;利用_set_printf_count_output函数能够启用该功能。
附录2
The optional prefixes to type, h, l, I, I32, I64, and ll specify
the "size" of argument (long or short, 32- or 64-bit, single-byte character or wide character, depending upon the type specifier that they modify). These type-specifier prefixes are used with type characters in printf functions
or wprintf functions to specify interpretation of arguments, as shown in the following table. These prefixes are Microsoft extensions and are not ANSI-compatible.
|
NOTE: |
|---|
|
The h and l prefixes are Microsoft extensions when used with data of type char. |
|
To specify |
Use prefix |
With type specifier |
|---|---|---|
|
long int |
l (lowercase L) |
d, i, o, x, or X |
|
long unsigned int |
l |
o, u, x, or X |
|
long long |
ll |
d, i, o, x, or X |
|
short int |
h |
d, i, o, x, or X |
|
short unsigned int |
h |
o, u, x, or X |
|
__int32 |
I32 |
d, i, o, x, or X |
|
unsigned __int32 |
I32 |
o, u, x, or X |
|
__int64 |
I64 |
d, i, o, x, or X |
|
unsigned __int64 |
I64 |
o, u, x, or X |
|
ptrdiff_t (that is, __int32 on 32-bit platforms, __int64 on 64-bit platforms) |
I |
d, i, o, x, or X |
|
size_t (that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms) |
I |
o, u, x, or X |
|
long double |
l or L |
f |
|
Single-byte character with printf functions |
h |
c or C |
|
Single-byte character with wprintf functions |
h |
c or C |
|
Wide character with printf functions |
l |
c or C |
|
Wide character with wprintf functions |
l |
c or C |
|
Single-byte – character string with printf functions |
h |
s or S |
|
Single-byte – character string with wprintf functions |
h |
s or S |
|
Wide-character string with printf functions |
l |
s or S |
|
Wide-character string with wprintf functions |
l |
s or S |
|
Wide character |
w |
c |
|
Wide-character string |
w |
s |
Thus to print single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.
|
To print character as |
Use function |
With format specifier |
|---|---|---|
|
single byte |
printf |
c, hc, or hC |
|
single byte |
wprintf |
C, hc, or hC |
|
wide |
wprintf |
c, lc, lC, or wc |
|
wide |
printf |
C, lc, lC, or wc |
To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S.
C++中C/C++格式化输出的更多相关文章
- Java中数字的格式化输出
Java中数字的格式化输出 double d = 345.678; String s = "hello!"; int i = 1234; //"%"表示进行格式 ...
- MYSQL 、Oracle、SQLServer 数据库中时间的格式化输出
在MYSQL 中格式化输出 date_forma t(date,'yyyyMMddHHmmss') Oracle 中格式化输出 to_char(time ,'yyyyMMddHHmmss') SQL ...
- Python中日志的格式化输出
import logging logfile = 'e:\\a.txt' # logging.basicConfig(filename=logfile,level=logging.INFO) # lo ...
- Objective-C中的字符串格式化输出(转载)
转自:http://www.cnblogs.com/jackbutler/archive/2012/04/05/2432828.html %@ 对象 %d, %i 整数 %u 无符整形 %f 浮点/双 ...
- Python print格式化输出
python中的print格式化输出,基本格式:"[字符串]%格式1[字符串]%格式2[字符串]....."%(string1,string2.....) 格式符号 ------- ...
- python -- while循环,格式化输出,运算符,初识编码
一.while循环 1.语法 while 条件: 循环体(结果) 如果条件为真,则直接执行结果),然后再次判断条件,知道条件为假,停止循环. while True: print('你是谁呢') 退 ...
- day_02 循环格式化输出编码运算符
1.while循环 语法 while 条件: 循环体 else: 当条件不成立的时候执行这里,和break没关系 如果循环是通过break退出的. 那么while后面的else将不会被执行, 只有在w ...
- day04-交互、格式化输出及基本运算符
目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...
- C语言中格式化输出的转换说明的fldwidth和precision解析
首先说什么是C语言的格式化输出,就是printf和它的几个变种(grep -E "v?(sn|s|f)printf").像这些函数都有一个参数format,format中可以加点转 ...
随机推荐
- linux下ubuntu系统安装及开发环境配置
1.安装系统:别的没什么说的,就是安的时候把网线拔了,不然到 configure apt的时候会卡起很久不走的2.配置网络 编辑/etc/network/interface打开/etc/networt ...
- git/github初级运用自如(zz)
----//git/github环境配置 一 . github上创建立一个项目 用户登录后系统,在github首页,点击页面右下角“New Repository” 填写项目信息: project n ...
- ASCII和16进制
所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...
- The serializable class does not declare a static final serialVersionUID field of type long
在编译以下Java程序时,出现The serializable class does not declare a static final serialVersionUID field of typ ...
- 大数据时代的数据存储,非关系型数据库MongoDB
在过去的很长一段时间中,关系型数据库(Relational Database Management System)一直是最主流的数据库解决方案,他运用真实世界中事物与关系来解释数据库中抽象的数据架构. ...
- enum 使用
1.说明 enum是一个基本的关键字,却一直没弄清楚怎么用,这次在实现二叉树框架时需要用到常量,特地搜了一下,终于知道怎么用了. 2.enum使用要点 enum声明是一个类型,不是变量. enum经常 ...
- Camera拍照声设定
在某些国家(比如Japan),为了防止偷拍,强制拍照声是需要从Speaker出来的(即使插入耳机的情况下). 实现该功能比较简单的方法就是将拍照声类型设置为Ringtone 或 Alarm 或 Not ...
- windows端口被占用
查看端口号被占用进程netstat -a -n -o 强制结束PIDtaskkill /pid:604 /F
- jquery cookie用法(获取cookie值,删除cookie)
1.引入文件 2.具体操作 $.cookie('the_cookie'); // 读取 cookie $.cookie('the_cookie', 'the_value'); // 存储 cookie ...
- 第二百一十五、六天 how can I 坚持
昨天刷机刷到很晚,博客都忘写了,刷了个flyme,用着没什么感觉,今天打电话试了下还有破音,有点小后悔.不行过两天再刷回来. 今天.mysql ifnull函数. 两条熊猫鱼都死了,这两天雾霾那么严重 ...