前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sprintf_s的使用

sprintf_s的使用

作者头像
青木
发布2018-05-28 15:25:40
3.3K0
发布2018-05-28 15:25:40
举报

syntax

代码语言:javascript
复制
int sprint_s(
    char *buffer,
    size_t sizeOfBuffer,
    const char *format
)
/*
header:stdio.h
*/

Parameters

  • buffer Storage location for output
  • sizeOfBuffer Maximum number of character to store.
  • format Format-control string

Return Value

  • The number of characters written,or -1 if an error occurred.If buffer or format is a null pointer.
  • sprintf_s returns the number of bytes stored in buffer,not counting the terminating null character.

Remarks

The sprintf_s *function formats and stores a series of characters and values in buffer.Each argument(if any) is coverted and output according to the corresponding format specification in format. The format consists of ordinary character and has the same form and function as the format argument for printf.A null character is appended after the last chatacter written. If copying occurs between strings that overlap,the behavior is undefined.

Example

代码语言:javascript
复制
// crt_sprintf_s.c
// This program uses sprintf_s to format various
// data and place them in the string named buffer.
//
#include <stdio.h>
int main( void )
{
   char  buffer[200], s[] = "computer", c = 'l';
   int   i = 35, j;
   float fp = 1.7320534f;
   // Format and print various data: 
   j  = sprintf_s( buffer, 200,     "   String:    %s\n", s );
   j += sprintf_s( buffer + j, 200 - j, "   Character: %c\n", c );
   j += sprintf_s( buffer + j, 200 - j, "   Integer:   %d\n", i );
   j += sprintf_s( buffer + j, 200 - j, "   Real:      %f\n", fp );
   printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output

Output: String: computer Character: l Integer: 35 Real: 1.732053 character count = 79

代码语言:javascript
复制
// crt_swprintf_s.c
// wide character example
// also demonstrates swprintf_s returning error code
#include <stdio.h>
int main( void )
{
   wchar_t buf[100];
   int len = swprintf_s( buf, 100, L"%s", L"Hello world" );
   printf( "wrote %d characters\n", len );
   len = swprintf_s( buf, 100, L"%s", L"Hello\xffff world" );
   // swprintf_s fails because string contains WEOF (\xffff)
   printf( "wrote %d characters\n", len );
}
Output

wrote 11 characters wrote -1 characters

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • syntax
  • Parameters
  • Return Value
  • Remarks
  • Example
    • Output
      • Output
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档