snprintf函数使用

2023-10-31

int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...);

函数说明:最多从源串中拷贝n1个字符到目标串中,然后再在后面加一个0

函数返回值:若成功则返回写入的字符串长度,若出错则返回负值,注意,只有当这个返回值是非负的,并且小于n,才表明该字符串已被完全写入。

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

int main()
{
     char str1[10]={0,};
     snprintf(str1, sizeof(str), "01234567890123456");
     printf("str1=%s/n", str1);
     return 0;
}

结果
str1=012345678


附C++标准说明http://www.cplusplus.com/reference/cstdio/snprintf/

function
<cstdio>

snprintf

int snprintf ( char * s, size_t n, const char * format, ... );
Write formatted output to sized buffer
Composes a string with the same text that would be printed if  format was used on  printf, but instead of being printed, the content is stored as a  C string in the buffer pointed by  s (taking  n as the maximum buffer capacity to fill).

If the resulting string would be longer than  n-1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.

A terminating null character is automatically appended after the content written.

After the  format parameter, the function expects at least as many additional arguments as needed for  format.

Parameters

s
Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least  n characters.
n
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most  n-1, leaving space for the additional terminating null character.
size_t is an unsigned integral type.
format
C string that contains a format string that follows the same specifications as  format in  printf (see  printf for details).
...  (additional arguments)
Depending on the  format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a  format specifier in the  format string (or a pointer to a storage location, for  n).
There should be at least as many of these arguments as the number of values specified in the  format specifiers. Additional arguments are ignored by the function.

Return Value

The number of characters that would have been written if  n had been sufficiently large, not counting the terminating  null character.
If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less than  n, the string has been completely written.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* snprintf example */
#include <stdio.h>

int main ()
{
  char buffer [100];
  int cx;

  cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );

  if (cx>=0 && cx<100)      // check returned value

    snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 );

  puts (buffer);

  return 0;
}


Output:
The half of 60 is 30, and the half of that is 15.

For more examples on formatting see  printf.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

snprintf函数使用 的相关文章

随机推荐

  • 数字信号处理:重要知识点整理

    文章目录 0 最重要 DFT和FFT的区别 1 连续时间信号频域分析 2 通过离散时间信号的Z变换表达式X z 直接写出时域离散信号 序列 x n 的方法 3 部分分式法的MATLAB实现 求X z 的部分展开式 4 稳定系统 5 求频响特
  • javaFX安装及使用

    一 javaFX下载 Jdk 从 1 8 开始 jdk 自带 JavaFX 到 Jdk 11 开始 jdk 不再内置 JavaFX 所以如果你使用的是高版本的 jdk 在使用 javaFX 的时候就需要先安装 javaFX 并在 idea
  • 在 Python 中打印没有逗号和括号的列表

    文章目录 使用 sep 打印不带逗号和括号的列表 在 Python 中打印不带括号的列表 使用 sep 打印不带括号的列表 Python 中要打印不带逗号和括号的列表 使用 str join 方法将列表连接成一个字符串 如果列表包含数字 将
  • Java源码分析(一)Integer

    当你掌握Java语言到了一定的阶段 或者说已经对Java的常用类和API都使用的行云流水 你会不会有一些思考 比如 这个类是如何设计的 这个方法是怎么实现的 接下来的一系列文章 我们一起学习下Java的一些常见类的源码 本篇 一起分析下In
  • struts2线程安全

    问题 Struts 2 Action对象为每一个请求产生一个实例 因此没有线程安全问题 Spring的Ioc容器管理的bean默认是单实例的 上一次请求处理的状态信息被保持下来 并影响了下一次的请求 实际上就是Action中的类变量被不同的
  • Mybatis 的部分优化及注意事项

    一 加载数据库时可以编写外部文件通过导入的方式获取 1 外部资源 driver com mysql jdbc Driver url jdbc mysql localhost 3306 mybatis userSSL true useUnco
  • 求全排列(JAVA)

    输出自然数1输出自然数1到n所有不重复的排列 即n的全排列 到n所有不重复的排列 即n的全排列 package n的全排列 import java util Scanner author admin public class Main st
  • 【HTML+CSS+JS】登录注册页面大合集

    前言 学JS也学了一段时间 正巧碰上了人工智能要调用人脸识别接口进行真人人脸识别 于是便萌生了用人脸来进行注册和登录的想法 这样的话就需要开发一个登录注册页面 然后用JS绑定注册事件调用人脸识别接口进行登录注册 饭要一口一口吃 路要一步一步
  • Vue项目启动内存溢出 js stack overflow

    方法1 在node modelus目录下的 vue cli service bin vue cli service js文件中的首行加入 usr bin env node max old space size 4096 方法二 修改本地项目
  • 入门卷积神经网络必备,基础、理论、实战一网打尽!

    目录 前言 教程安排 1 基本概念 2 论文解读 3 模型结构与原理 4 模型实战 5 问题解答 6 目标检测领域 相关技术群 前言 其实 挺早之前就有更新有关于卷积神经网络文章的想法 这些时间里 因为一些事情 更新进度确实慢了一些 但是也
  • 三星显示器服务器网络设置方法,三星显示器也有这样的通病,看完你就知道怎么解决...

    原标题 三星显示器也有这样的通病 看完你就知道怎么解决 小编今天在网上看到一个教程说三星显示器能开机 关键是屏幕还会提示无信号输入 看完非常兴奋 因为小编店里就有一台三星的显示器出现一模一样的故障 就是开机接上电脑后提示无信号输入 但是显示
  • Java Redis三种客户端对比(优缺点对比+使用建议)

    Redis的Java客户端很多 官方推荐的有三种 Jedis Redisson和lettuce 在这里对Jedis和Redisson进行对比介绍 Jedis 轻量 简洁 便于集成和改造 支持连接池 支持pipelining 事务 LUA S
  • 12. 微积分 - 梯度&积分

    Hi 大家好 我是茶桁 上一节课 我们讲了方向导数 并且在最后留了个小尾巴 是什么呢 就是梯度 我们再来回看一下但是的这个式子 f x f y
  • matlab练习程序(圆柱投影)

    圆柱投影就是将一张二维的图像投影到三维的圆柱体上 不过在显示图像的时候依然是以二维的形式给出 投影最重要的步骤就是计算投影变换公式 和图像旋转类似 只要得到变换公式 再依照公式进行代码编写就很容易了 这里就不写投影变换公式的推导过程了 直接
  • 区块链在金融领域的应用案例

    区块链这一颠覆性的技术作为当下多种热门概念的交集 将在可预见的未来深刻地影响包括金融业在内的多个行业 制造跨领 域合作的机会 提高资源配置效率 助力产业转型升级 对区块链技术的积极应对或是被动接受 也将导致各行业内部的重新洗牌 在金融领域
  • vs2010main.cpp不能检测到main.h

    今天想更改在github找到的人脸识别代码 主要想添加一个main h在原有代码中 并且把main cpp的一些东西挪到 h中 出现问题 在解决方案中添加了main h 但是在 cpp中添加 include main h 时出现错误 系统找
  • QT窗体禁止拖动缩放:使用setFixedSize方法

    QT窗体禁止拖动缩放 使用setFixedSize方法 需求 我想实现窗体在正常状态 Qt WindowNoState 边框不能通过鼠标拖动改变窗体大小 不影响窗体的正常最大化和还原状态 1024 768 屏幕的初始大小是可使用屏幕的大小
  • Qt5 安装教程

    Qt 是一个支持windows linux android等系统平台的集成开发环境 可以作为C 软件开发界面设计及代码编写的开发工具 并且其具有诸多基于C 底层封装的类库 对于新手程序猿来说 使用起来比较友好 1 官网下载需要安装的版本 h
  • vue 显示txt显示到页面_CDR页面“外”的内容在每个页面外都显示的方法

    前言 正常情况下 CDR软件中页面外的为 桌面 在哪个页面可以看到的 但是有粉丝反应 在CDR 2019版本中 将第一页的内容拖出页面以外时 在第二页面是看不到这些内容的 X4版有时也会这样 今天小编给大家分享CDR X4和2019版页面
  • snprintf函数使用

    int snprintf char restrict buf size t n const char restrict format 函数说明 最多从源串中拷贝n 1个字符到目标串中 然后再在后面加一个0 函数返回值 若成功则返回写入的字符