c中printf语句中的%.#s格式说明符 [英] %.#s format specifier in printf statement in c

查看:78
本文介绍了c中printf语句中的%.#s格式说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释输出.printf()中的%.#s是什么意思?

Please explain the output. What does %.#s in printf() mean?

#include<stdio.h>
#include <stdlib.h>

int main(int argc,char*argv[]){

    char *A="HELLO";
    printf("%.#s %.2s\n",A,A);
    return 0;
}

输出:

#s HE

推荐答案

这是未定义的行为.#printf 格式说明符中表示替代形式,但根据标准,# 仅与o 一起使用、aAxXeEfFgG,不包括s>.

It's undefined behavior. # in printf format specifier means alternative form, but according to the standard, # is only used together with o, a, A, x, X, e, E, f, F, g, G, not including s.

# 结果被转换为替代形式".对于 o 转换,它增加精度,当且仅在必要时,强制结果的第一位数字为零(如果值和精度都是 0,则打印单个 0).对于 x(或 X)转换,非零结果带有 0x(或 0X)前缀.对于aAeEfFgG 转换,总是转换浮点数的结果包含小数点字符,即使后面没有数字.(通常,这些转换的结果中才会出现小数点字符,前提是它后面跟着一个数字.)对于 gG 转换,尾随零不会从结果.对于其他转换,行为未定义.

C11 §7.21.6.1 The fprintf function Section 6

# The result is converted to an ‘‘alternative form’’. For o conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single 0 is printed). For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to it. For a, A, e, E, f, F, g, and G conversions, the result of converting a floating-point number always contains a decimal-point character, even if no digits follow it. (Normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) For g and G conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.

例如,在我的机器上,输出是不同的:%.0#s HE

For example, on my machine, output is different: %.0#s HE

这篇关于c中printf语句中的%.#s格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆