c语言中sprintf函数

In this article, we’ll take a look at using the sprintf() function in C / C++.

在本文中,我们将研究在C / C ++中使用sprintf()函数。

This is an important function if you want to write characters from a format string into a buffer.

如果要将字符从格式字符串写入缓冲区,这是一项重要功能。

Let’s take a look at this function, using illustrative examples!

让我们使用示例说明一下此功能!



C / C ++中sprintf()函数的基本语法 (Basic Syntax of the sprintf() function in C/C++)

This function takes a format string and writes it into a string buffer.

此函数采用格式字符串,并将其写入字符串缓冲区。

This is very similar to printf(), but writes the output to a buffer instead of stdout.

这与printf()非常相似,但是将输出写入缓冲区而不是stdout

This is function is present in the <stdio.h> header file.

该功能存在于<stdio.h>头文件中。


#include <stdio.h>int sprintf (char* buffer, const char* fmt, ...);

Here, this takes in an input fmt string along with necessary arguments (denoted by ...) and stores it into buffer.

在这里,这将接收输入的fmt字符串以及必要的参数(由...表示),并将其存储到buffer

The function returns the number of characters written to the buffer if it succeeds.

如果成功,该函数将返回写入缓冲区的字符数。

If it fails, it will return a negative integer.

如果失败,它将返回一个负整数。

The size of the buffer must be large enough to accomodate the string! Otherwise, you can access an unbound memory location.

缓冲区的大小必须足够大以容纳字符串! 否则,您可以访问未绑定的内存位置。

Now, to understand this completely, let’s look at some examples:

现在,为了完全理解这一点,让我们看一些示例:



使用sprintf()–一些示例 (Using sprintf() – Some Examples)

Let’s take an input from the user, and concatenate it onto a string, using sprintf().

让我们从用户那里获取输入,并使用sprintf()将其连接到字符串上。


#include <stdio.h>int main() {char original[] = "Hello from JournalDev";char buffer[256];printf("Enter an integer:\n");int n;scanf("%d", &n);sprintf(buffer, "%s_%d", original, n);printf("Buffer = %s\n", buffer);return 0;
}

Output

输出量


Enter an integer:
100
Buffer = Hello from JournalDev_100

As you can see, we used the format string to concatenate an integer to a string directly.

如您所见,我们使用格式字符串将整数直接连接为字符串。

You may just realize how handy this function can be when used at the right time!

您可能只是意识到在正确的时间使用该功能非常方便!

Let’s now take another example to verify that all the characters are written to the buffer.

现在让我们举另一个例子来验证是否所有字符都已写入缓冲区。


#include <stdio.h>int main() {char original[] = "Hello from JournalDev";char buffer[25];printf("Enter an integer:\n");int n;scanf("%d", &n);int num_written = sprintf(buffer, "%s_%d", original, n);printf("Buffer = %s\n", buffer);printf("Number of Characters written using sprintf() = %d\n", num_written);return 0;
}

Output

输出量


Enter an integer:
10
Buffer = Hello from JournalDev_10
Number of Characters written using sprintf() = 24

As you can see, the number of characters written (24), is the same as the string length!

如您所见,写入的字符数(24)与字符串长度相同!



结论 (Conclusion)

In this article, we learned about using the sprintf() function in C/C++. This is useful if you want to write to a string using a format specifier.

在本文中,我们学习了在C / C ++中使用sprintf()函数的知识。 如果要使用格式说明符写入字符串,这将很有用。

For similar content, do go through our tutorial section on C programming, which covers various aspects of C and C++.

对于类似的内容,请阅读我们有关C编程的教程部分 ,其中涵盖C和C ++的各个方面。

参考资料 (References)

  • cppreference.com page on sprintf() in C / C++C / C ++中sprintf()上的cppreference.com页面


翻译自: https://www.journaldev.com/41098/sprintf-function-in-c-plus-plus

c语言中sprintf函数

c语言中sprintf函数_在C / C ++中使用sprintf()函数相关推荐

  1. matlab中floor函数,floor函数_怎么在excel中使用floor函数

    floor函数即上取整函数,是计算机C语言中的数学函数,与ceil函数相对应.但是它在excel中却是另一种含义,FLOOR函数是向下舍入为最接近指数基数的倍数,下面小编就教你怎么在excel中使用f ...

  2. 原生js已载入就执行函数_手写CommonJS 中的 require函数

    前言 来自于圣松大佬的文章<手写CommonJS 中的 require函数> 什么是 CommonJS ? node.js 的应用采用的commonjs模块规范. 每一个文件就是一个模块, ...

  3. python中add函数_如何使用python中的add函数?

    之前向大家介绍过python中的求和函数sum函数,numpy中的sum函数,对于数组可以指定维度进行相加.numpy中还有另一种求和运算方法,即add函数.add函数不仅作用于numpy中加法运算, ...

  4. python中index函数_详解python中的index函数用法

    1.函数的创建 def fun(): #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun() #执行函数 2.函数的参数 普通参数 :要按照顺序输入参数 de ...

  5. mounted钩子函数_怎样实现Vue中mounted钩子函数获取节点高度

    这次给大家带来怎样实现Vue中mounted钩子函数获取节点高度,实现Vue中mounted钩子函数获取节点高度的注意事项有哪些,下面就是实战案例,一起来看一下. 遇到的问题 最近在开发一个Vue的项 ...

  6. python grid函数_详解numpy中的meshgrid函数用法

    numpy中的meshgrid函数的使用 numpy官方文档meshgrid函数帮助文档https://docs.scipy.org/doc/numpy/reference/generated/num ...

  7. python hasattr函数_浅谈python中的getattr函数 hasattr函数

    hasattr(object, name) 作用:判断对象object是否包含名为name的特性(hasattr是通过调用getattr(ojbect, name)是否抛出异常来实现的). 示例: & ...

  8. python numpy sum函数_如何使用Python中的sum函数?

    之前小编向大家介绍过python中的sum函数(https://www.py.cn/jishu/jichu/22025.html).在python中sunm函数使用分为两种情况,一种是python自带 ...

  9. python中从小到大排序的函数_深入理解Python中的排序函数

    由于 Python2 和 Python3 中的排序函数略有区别,本文以Python3为主. Python 中的排序函数有 sort , sorted 等,这些适用于哪些排序,具体怎么用,今天就来说一说 ...

最新文章

  1. 深度学习很难?一文读懂深度学习!
  2. 【Python】青少年蓝桥杯_每日一题_10.27_日期差值的判断
  3. 将jar文件转换成exe可执行文件[转]
  4. scrapy 中不同页面的拼接_scrapy使用技巧总结
  5. img文件编辑_只会用chmod 777?Linux下的文件权限居然还有这么多骚操作
  6. 来客推仿拼多多电商商城小程序源码
  7. iOS中使用UIActivityViewController系统分享到微信QQ微博包含文字图片URL
  8. 蓝桥杯 2017年C语言组大学B组 C/C++
  9. python三本经典书籍-有哪些 Python 经典书籍?
  10. 建议0 不要让main函数返回void
  11. JavaSE教程_1 安装jdk
  12. 关于ctf竞赛训练 积累的资料
  13. Delphi android 开发视频教程
  14. php验证码点击刷新,php点击可刷新验证码
  15. Windows安全加固系列---日志配置操作
  16. postman:Tests模块之断言
  17. C++11中的原子操作(atomic operation)和自旋锁
  18. POJ 3278,抓牛问题(BFS)
  19. 水星mac1300r虚拟服务器,水星(MERCURY)MAC1300R路由器用手机怎么设置?
  20. 贪心算法解决商场买赠问题(最简单,通俗易懂,附C++代码

热门文章

  1. 移动端手势的七个事件库
  2. qrcodeJS生成二维码
  3. 蓝桥杯——说好的进阶之入学考试
  4. [转载] python创建集合set()_Python 集合set()
  5. [转载] python的短逻辑
  6. Vue.js 学习笔记 九 v-if和v-show
  7. Python中,os.listdir遍历纯数字文件乱序如何解决
  8. 分享:Python中的位运算符
  9. 取消冒泡的兼容性写法
  10. JS继承之寄生类继承