【Learn CPP】printf 整理
01 简介
02 printf()
03 snprintf()
snprintf() 函数 在 <stdio.h> 头文件中定义,用于以指定格式存储指定字符串直至指定长度。
1 | int snprintf(char * s, size_t n, const char *format, …); |
[!quote]
Thesnprintf()function is defined in the <stdio.h> header file and is used to store the specified string till a specified length in the specified format.
Characteristics ofsnprintf()method:
- The snprintf() function formats and stores a series of characters and values in the array buffer.
- The snprintf() function accepts an argument ‘n’, which indicates the maximum number of characters (including at the end of null character) to be written to buffer.
- The snprintf() function is used to redirect the output of printf() function onto a buffer.
- The snprintf() also returns the number characters that were supposed to be written onto the buffer (excluding the null terminator), irrespective of the value of ‘n’ passed.
- So, only when the returned value is non-negative and less than ‘n’, the string has been completely written as expected.
3.1 Example
3.1.1 Example
1 | // C program to demonstrate snprintf() |
Output:
1 | Writing geeksforgeeks onto buffer with capacity 6 |
3.1.2 Example: MicroROS
1 | std_msgs__msg__String pub_msg; |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 咕咕评客!
评论



