C语言 printf 函数 - C语言零基础入门教程 ゞ 浴缸里的玫瑰 2022-09-02 11:53 189阅读 0赞 目录 * 一.printf 函数简介 * 二.C 语言格式符/占位符 * 三.printf 函数实战 * 四.猜你喜欢 > 零基础 C/C++ 学习路线推荐 : [C/C++ 学习目录][C_C_] >> [C 语言基础入门][C] [printf 函数][printf]是 C 语言标准库函数和 [Python 内置函数][Python]类似,主要用于将格式化后的字符串输出到终端的屏幕([C 语言入门 Hello World][C _ Hello World])。使用 printf 函数需要包含头文件 stdio.h \*\* 值得注意的是:需要注意\#include” “与\#include <>区别;\*\* /************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言 printf 函数 //@Time:2021/05/25 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /************************************************************************/ //写法一:推荐 #include <stdio.h> //写法二:不推荐 #include "stdio.h" \*\* \#include <stdio.h> 会直接在系统目录搜索 stdio.h ,如果系统目录也搜索不到,直接报错:No such file or directory!\*\* \*\* include “stdio.h” 首先在工程目录搜索 stdio.h ,如果工程目录搜索不到,会继续在系统目录搜索 stdio.h ,如果系统目录也搜索不到,直接报错:No such file or directory!\*\* \*\* 对比可以发现:虽然第一种写法和第二种写法效果一样,但是第二种查找头文件的时候更耗时间,所以,系统的头文件推荐使用第一种写法!\*\* ## 一.printf 函数简介 ## [printf 函数][printf]是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。在编写程序时经常会用到此函数。函数的原型为: /************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言 printf 函数 //@Time:2021/05/25 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /************************************************************************/ //返回值: 返回一个整型,若成功则返回输出的字符数,输出出错则返回负值。 int printf(const char *format, …); printf("<格式化字符串>", <参量表>); ## 二.C 语言格式符/占位符 ## 在[ C 语言][C]中格式控制符/占位符 中也有对格式符有作简单的讲解: * %d 十进制有符号整数 * %u 十进制无符号整数 * %f 浮点数 * %s 字符串 * %c 单个字符 * %p 指针的值 * %e 指数形式的浮点数 * %x, %X 无符号以十六进制表示的整数 * %o 无符号以八进制表示的整数 * %g 把输出的值按照 %e 或者 %f 类型中输出长度较小的方式输出 * %p 输出地址符 * %lu 32 位无符号整数 * %llu 64 位无符号整数 ## 三.printf 函数实战 ## /************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言 printf 函数 //@Time:2021/05/25 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /************************************************************************/ include "stdafx.h" include "stdio.h" include "stdlib.h" int main() { char ch = 'B'; char str[20] = "www.codersrc.com"; float flt = 3.1415; int num = 150; double dbl = 20.123456; printf("字符为 %c \n", ch); printf("字符串为 %s \n", str); printf("浮点数为 %f \n", flt); printf("整数为 %d\n", num); printf("双精度值为 %lf \n", dbl); printf("八进制值为 %o \n", num); printf("十六进制值为 %x \n", num); return 0; } /* 输出结果: 字符为 B 字符串为 www.codersrc.com 浮点数为 3.141500 整数为 150 双精度值为 20.123456 八进制值为 226 十六进制值为 96 请按任意键继续. . . */ 同样 printf 函数支持同时输出多个参数: char ch = 'B'; char str[20] = "www.codersrc.com"; float flt = 3.1415; printf("字符为 %c 字符串为 %s 浮点数为 %f \n", ch,str,flt); //输出: 字符为 B 字符串为 www.codersrc.com 浮点数为 3.141500 ## 四.猜你喜欢 ## 1. [安装 Visual Studio][Visual Studio] 2. [安装 Visual Studio 插件 Visual Assist][Visual Studio _ Visual Assist] 3. [Visual Studio 2008 卸载][Visual Studio 2008] 4. [Visual Studio 2003/2015 卸载][Visual Studio 2003_2015] 5. [设置 Visual Studio 字体/背景/行号][Visual Studio 1] 6. [C 语言 Hello World][C _ Hello World] 7. [C 语言代码注释][C 1] 8. [C 语言数据类型 / 变量类型][C _ _] 9. [C 语言变量声明和定义][C 2] 10. [C 语言格式控制符/占位符][C 3] 11. [C 语言 printf 函数][printf] 未经允许不得转载:[猿说编程][Link 1] » [C 语言 printf 函数][printf] [C_C_]: https://www.codersrc.com/c-c [C]: https://www.codersrc.com/c%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80 [printf]: https://www.codersrc.com/archives/7464.html [Python]: https://www.codersrc.com/python%e5%b8%b8%e7%94%a8%e5%87%bd%e6%95%b0 [C _ Hello World]: https://www.codersrc.com/archives/7387.html [Visual Studio]: https://www.codersrc.com/archives/7250.html [Visual Studio _ Visual Assist]: https://www.codersrc.com/archives/7280.html [Visual Studio 2008]: https://www.codersrc.com/archives/7288.html [Visual Studio 2003_2015]: https://www.codersrc.com/archives/7292.html [Visual Studio 1]: https://www.codersrc.com/archives/7284.html [C 1]: https://www.codersrc.com/archives/7404.html [C _ _]: https://www.codersrc.com/archives/7409.html [C 2]: https://www.codersrc.com/archives/7426.html [C 3]: https://www.codersrc.com/archives/7460.html [Link 1]: https://www.codersrc.com
还没有评论,来说两句吧...