c语言snprintf函数_带示例的C语言snprintf()函数

c语言snprintf函数

C语言中的snprintf()函数 (snprintf() function in C)

The snprintf() function is defined in the <stdio.h> header file.

snprintf()函数在<stdio.h>头文件中定义。

Prototype:

原型:

    int snprintf(char *str, size_t size, const char *format, ...);

Parameters:

参数:

  • str - is a Buffer.

    str-是一个缓冲区。

  • size - is a maximum number of bytes.

    size-是最大字节数。

  • format - c string that contains a format that follows the same specifications as format in printf.

    format -c字符串,其格式遵循与printf中相同的格式。

  • ... - the optional (...) arguments are just the string formats like “%d” as seen in printf.

    ...-可选的( ... )参数只是字符串格式,如printf中所示的“%d”。

Return type: int

返回类型: int

Use of function:

使用功能:

  • The snprintf() formats and stores a series of characters and values in the array buffer.

    snprintf()格式化并将一系列字符和值存储在数组缓冲区中。

  • It redirects the output of printf to the buffer.

    它将printf的输出重定向到缓冲区。

  • Using snprintf() build a string once and use %s instead of %d, %s, %f, %ld every time.

    使用snprintf()一次构建一个字符串,并每次使用%s代替%d , %s , %f和%ld 。

C中的snprintf()示例 (snprintf() example in C)

#include <stdio.h> 

int main() { 
	char* r = "Includehelp.com"; 
	char buf[100]; 
	
	// in buffer using snprintf 
	int i = snprintf(buf, 12, "%s\n", r); 
	
	// Print the string stored in buffer and 
	// character count 
	printf("string is:\n%s\ncharacter count = %d\n", buf, i); 
	
	return 0; 
} 

Output

输出量

string is:
Includehelp
character count = 16


翻译自: https://www.includehelp.com/c-programs/snprintf-function-in-c-language-with-example.aspx

c语言snprintf函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值