printf和sprintf
时间: 2023-10-14 13:05:07 浏览: 40
printf()和sprintf()函数都是C语言中的标准库函数,用于将格式化后的字符串输出到不同的位置。其中,printf()函数将格式化后的字符串输出到标准输出,即终端屏幕;而sprintf()函数将格式化后的字符串输出到指定的字符数组中。
具体来说,printf()函数的声明位于头文件<stdio.h>中,其调用格式为printf("<格式化字符串>", <参数表>)。该函数返回打印出的字符数,不包括字符串末尾的空字符。示例代码如下:
```c
#include <stdio.h>
int main() {
int n;
n = printf("I am printf()\n");
printf("n = %d\n", n);
return 0;
}
```
上述代码中,printf()函数输出"I am printf()"并返回打印出的字符数,然后在下一行打印出该字符数。
而sprintf()函数的声明也位于头文件<stdio.h>中,其调用格式为sprintf("<字符串指针>", "<格式化字符串>", <参数表>)。该函数将格式化后的字符串写入指定的字符数组中,并返回写入的字符总数。示例代码如下:
```c
#include <stdio.h>
int main() {
int n;
char a<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [printf 和sprintf](https://blog.csdn.net/baidu_16370559/article/details/121746323)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [printf()和sprintf()](https://blog.csdn.net/qq_43668247/article/details/107813294)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)