snprintf函数使用

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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值