snprintf函数使用

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

函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个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;
}

Edit
& Run

Output:

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

For more examples on formatting see printf.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-11 10:21:07

snprintf函数使用的相关文章

【C语言天天练(十三)】printf、fprintf、sprintf和snprintf函数

#include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, ...); int sprintf(char *str, const char *format, ...); int snprintf(char *str, size_t size, const char *format, ...); printf是标准的输出函数. fprintf传送格式化输

sprintf和snprintf函数

printf()/sprintf()/snprintf()区别  先贴上其函数原型 printf( const char *format, ...)    格式化输出字符串,默认输出到终端-----stdout sprintf(char *dest, const char *format,...)     格式化输出字符串到指定的缓冲区 snprintf(char *dest, size_t size,const char *format,...)     按指定的SIZE格式化输出字符串到指定

转载--snprintf函数用法

作者:crfoxzl 链接:http://blog.csdn.net/crfoxzl/article/details/2062139 int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...); 函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0.所以如果目标串的大小为n 的话,将不会溢出. 函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值. Result1(推荐的用

snprintf函数的用法

int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...); 函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0.所以如果目标串的大小为n 的话,将不会溢出. 函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值. Result1(推荐的用法) #include <stdio.h>#include <stdlib.h> int main(){     cha

【转】snprintf()函数使用方法

众所周知,sprintf不能检查目标字符串的长度,可能造成众多安全问题,所以都会推荐使用snprintf. 注:sprintf()函数:int sprintf( char *buffer, const char *format, [ argument] … );返回的是实际写入buffer的长度 自从snprintf代替了sprintf,相信大家对snprintf的使用不会少,函数定义如下: int snprintf(char* dest_str,size_t size,const char*

snprintf函数用法

函数原型 int snprintf(char *str, size_t size, const char *format, ...) 功能 将可变个参数(...)按照format格式化成字符串,然后将其复制到str中 (1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0'): (2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入

c语言snprintf函数简介

函数原型:int snprintf(char* dest_str,size_t size,const char* format,...); 所需头文件:#include<stdio.h> 函数功能:先将可变参数 "-" 按照format的格式格式化为字符串,然后再将其拷贝至dest_str中. 注意事项:如果格式化后的字符串长度小于size,则将字符串全部拷贝至dest_str中,并在字符串结尾处加上'\0': 如果格式化后的字符串长度大于或等于size,则将字符串的(si

snprintf()函数使用方法

众所周知,sprintf不能检查目标字符串的长度,可能造成众多安全问题,所以都会推荐使用snprintf. 自从snprintf代替了sprintf,相信大家对snprintf的使用都不会少,函数定义如下: int snprintf(char*str, size_t size,constchar*format, ...); 函数说明: 最多从源串中拷贝size-1个字符到目标串中,然后再在后面加一个0.所以如果目标串的大小为size的话,将不会溢出. 函数返回值: 若成功则返回欲写入的字符串长度

snprintf函数

原型为:snprintf(char *str, size_t size, const char *format, ...) 将可变个参数(...)按照format格式化成字符串,然后将其复制到str中 (2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main