C库类 的 sprintf - C语言库函数

C库函数int sprintf(char *str, const char *format, ...)发送str 指向一个字符串的格式化输出。

声明

以下是 sprintf() 函数的声明。

intsprintf(char*str,constchar*format,...)

参数

str-- 这是C字符串存储char元素的数组的指针。

format-- 这是包含文本的字符串被写入到缓冲。它可以包含嵌入的格式在随后的附加参数指定的值所取代的标签和格式化所要求。格式标签的原型:%[flags][width][.precision][length]specifier, 详细说明如下::

修辞符

输出

c

Character.

d or i

Signed decimal integer

e

Scientific notation (mantissa/exponent) using e character

E

Scientific notation (mantissa/exponent) using E character

f

Decimal floating yiibai

g

Use the shorter of %e or %f.

G

Use the shorter of %E or %f

o

Signed octal

s

String of characters

u

Unsigned decimal integer

x

Unsigned hexadecimal integer

X

Unsigned hexadecimal integer (capital letters)

p

Yiibaier address

n

Nothing printed.

%

Character.

标识

描述

-

Left-justify within the given field width; Right justification is the default (see width sub-specifier).

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign..

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal yiibai even if no digits would follow. By default, if no digits follow, no decimal yiibai is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

width

描述

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

.precision

描述

.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers: this is the number of digits to be printed after de decimal yiibai. For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

.*

The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

length

描述

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

L

The argument is interpreted as a long double (only applies to floating yiibai specifiers: e, E, f, g and G).

additional arguments -- 根据格式字符串,函数可能会想到一系列的额外的参数,每个包含一个值,而不是插入的格式参数中指定的标记每个%,如果有的话。应该有相同数量的%预期值的标签的数量的这些参数的。

返回值

如果成功,则返回写入的字符的总数,不包括空字符结束的字符串附加,否则在发生故障的情况下,返回一个负数。

例子

下面的例子演示了如何使用 sprintf() 函数。

#include#includeintmain(){charstr[80];sprintf(str,"Value of Pi = %f",M_PI);puts(str);return(0);}

让我们编译和运行上面的程序,这将产生以下结果:

Value of Pi = 3.141593

C语言中positive用法,sprintf - C语言库函数相关推荐

  1. c语言中ndigit用法,求C语言中头文件及函数的含意的总分类

    ALLOC.H 说明内存管理函数(分配.释放等). ASSERT.H 定义 assert调试宏. BIOS.H 说明调用IBM-PC ROM BIOS子程序的各个函数. CONIO. H 说明调用DO ...

  2. C语言中within函数,vfprintf() - C语言库函数

    C库函数 int vfprintf(FILE *stream, const char *format, va_list arg)发送格式化输出到一个流使用传递给它的参数列表. 声明 以下是vfprin ...

  3. c语言中用了continue头文件,c语言中continue用法是什么?

    c语言中continue用法是什么? continue 语句的作用是跳过循环体中剩余的语句而强制进入下一次循环. continue语句只用在 while.for 循环中,常与 if 条件语句一起使用, ...

  4. c语言for什么意思,C语言中for用法是什么?

    C语言中for用法是: for 循环语句的一般形式为:for (表达式1; 表达式2; 表达式3) { 语句; } 首先要强调两点: 1) 表达式1.表达式2和表达式3之间是用分号;隔开的,千万不要写 ...

  5. c语言printf函数的作用,c语言中printf用法及其函数定义

    c语言中printf用法及其函数定义 发布时间:2020-04-09 10:51:14 来源:亿速云 阅读:354 作者:小新 今天小编给大家分享的是c语言中printf用法及其函数定义,很多人都不太 ...

  6. java 函数fun_c语言中fun用法详解_后端开发

    Java Dao层的作用_后端开发 Dao层叫数据访问层,属于一种比较底层,比较基础的操作,可以具体到对于某个表或某个实体的增删改查,其Dao层的作用是对数据库的访问进行封装,从而不涉及业务,实现解耦 ...

  7. c语言中funx的作用,c语言中fun用法详解

    C语言中fun用法详解 fun函数是自定义的C/C++语言函数,函数功能多样.该函数名为"函数"英文function的简写,一般在示例和试题中使用,通常在主函数中被调用. C/C+ ...

  8. C语言中const用法详解

    C语言中const用法详解 const修饰普通变量 const作用在于定义一个常量,比如const int a = 10,代表a的值不能被直接修改了,是一个常量,但仍可以通过指针的方式间接修改,如下图 ...

  9. c语言typedef的用法,C语言中typedef用法总结,看完就能像编程老手一样熟练运用...

    请看下文 C语言中typedef关键字应用比较常见,许多C语言初学者对它的用法不甚了解.事实上,我们可以用typedef来定义自己习惯使用的数据类型名称,可以替代自己所熟悉的基本类型.数组类型.指针类 ...

最新文章

  1. java 数组集合转换_Java 实例 – 集合转数组
  2. Knockout2.x:ko.dataFor()、ko.contextFor()使用
  3. Swift3.0语言教程字符串与文件的数据转换
  4. 07/11/20 资料整理
  5. how many libraries within Cambridge?
  6. python-map函数
  7. centos安装mysql wsl_如何在 Windows 10 中安装 WSL2 的 Linux 子系统
  8. VTK:图片之ImageAccumulate
  9. Webpack4-基本使用
  10. linux之scp命令
  11. C语言函数多个返回值
  12. 关于音频通信引擎接口便宜性的实验
  13. BCNF范式的判断和分解
  14. kali linux暴力破解攻击
  15. linux lxde桌面快捷键,LXde 桌面有两下啊
  16. java计算机毕业设计基于安卓Android的禁毒宣传APP
  17. 数据库范式讲解(1NF、2NF、3NF、BCNF)
  18. 全国计算机等级考试证书电子,全国计算机等级考试证书效力
  19. 盘点:2022年国内31省市数字经济发展相关政策、目标
  20. 常见六种热量高食物和饮料

热门文章

  1. R语言ggplot2可视化绘制一头奶牛、Linux下使用cowsay打印奶牛(cow)
  2. Python使用matplotlib可视化散点图、并在可视化图像的底部和右边添加边缘箱图(Marginal Boxplot)
  3. python使用numpy包编写自定义函数计算SMAPE(对称平均绝对百分比误差)指标Symmetric mean absolute percentage error、SMAPE指标解读、指标使用的注
  4. R语言使用dplyr包使用group_by函数、summarise函数和mutate函数计算分组下的均值、标准差、样本个数、以及分组均值的95%执行区间对应的下限值和上限值(Calculate CI)
  5. R语言使用ggradar包可视化基本雷达图(radar chart、蜘蛛图spider plot)、可视化单个数据对象的雷达图、自定义雷达图的线条类型、线条宽度、数据点大小、色彩等
  6. R语言使用ggpubr包的ggdotplot函数可视化水平棒棒糖图(自定义分组数据点色彩、自定义调色板、在两端添加点图的线段segments、整体排序从大到小、自定义数据点的大小、添加数值标签)
  7. pandas基于元组列表(list of tuples)、列表词典(dictionary of lists)、词典列表(list of dictionaries)构建dataframe数据实战
  8. Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
  9. R语言set.seed函数的意义及实战
  10. 机器学习特征工程之连续变量离散化:等频分箱