本文介绍了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 转换,尾随零不会从结果.对于其他转换,行为未定义.

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

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

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

08-01 09:14
查看更多