首页 > 其他分享 >【C语言】格式化输入/输出

【C语言】格式化输入/输出

时间:2024-03-21 23:02:33浏览次数:24  
标签:control 格式化 string format C语言 说明符 printf standard 输入

C语言格式化输入、输出

简介

Streams provide communication channels between files and programs.

提供文件和程序之间的通信通道。

All input and output is performed with streams, which are sequences of bytes.

所有输入和输出都是通过(字节序列)执行的。

When program execution begins, three streams are connected to the program automatically. Normally, the standard input stream is connected to the keyboard and the standard output stream is connected to the screen. Operation systems often often allow these streams be redirected to other devices. A third stream, the standard error stream, is connected to the screen. Error messages are output to the standard error stream.

当程序开始执行时,三个流会自动连接到程序。通常,标准输入流连接到键盘,标准输出流连接到屏幕。操作系统通常允许将这些流重定向到其他设备。第三个流,标准错误流,连接到屏幕。错误消息输出到标准错误流。

The standard input, standard output and standard error are manipulated using file pointers stdin, stdout and stderr.

standard stream file pointer devices
标准输入 stdin 键盘
标准输出 stdout 屏幕
标准错误 stderr 屏幕

使用printf函数格式化输出

Precise output formatting is accomplished with printf. Ever printf call contains a format control string that describes the output format. The format control string consists of conversion specifiers, flags, field widths, precisions and literal characters. Together with the percent sign %, these form conversion specifications.

精确的输出格式化是通过 printf 完成的。每个 printf 调用都包含一个描述输出格式的格式控制字符串。格式控制字符串由转换说明符标志字段宽度精度文字字符组成。 这些与百分号 % 一起形成转换规范。

函数 printf 的格式为:
printf ( format-control-string , other-arguments ) ; {\text{printf}{\left({\textit{format-control-string},{\textit{other-arguments}}} \right)}}; printf(format-control-string,other-arguments);

format-control-string describes the output format, and other-arguments(which are optional) correspond to each conversion specification in format-control-string. Each conversion specification begins with a precent sign and ends with a conversion specifier. There can be many conversion specifications in the format control string.

format-control-string 描述输出格式,other-arguments(可选)对应于 format-control-string 中的每个转换规范。每个转换规范都以前置符号开始,并以转换说明符结束。格式控制字符串中可以有多种转换规范。

整数转换说明符

Integer Conversion Specifiers Explanation
d or I 显示有符号的十进制整数。
o 显示无符号八进制整数。
u 显示无符号十进制整数。
x or X 显示无符号的十六进制整数。 “X”显示为大写,“x”显示为小写。
h or l Place before any integer conversion specifier to indicate that a short or long integer is displayed respectively. Letters h and l are more precisely call length modifiers.放置在任何整数转换说明符之前,以指示分别显示 shortlong 整数。 字母 hl 更准确地称为长度修饰符

浮点数转换说明符

Float

标签:control,格式化,string,format,C语言,说明符,printf,standard,输入
From: https://blog.csdn.net/m0_59577534/article/details/136753634

相关文章

  • 【C语言】文件读写
    Files&Streams访问文件顺序访问文件随机访问文件创建随机访问文件修改随机访问文件读取随机访问文件访问文件Programsmayprocessnofiles,onefileorseveralfiles.Eachfileusedinaprogrammusthaveauniquenameandwillhaveadiffe......
  • C语言解决水仙花问题
    题目叙述:水仙花数是 指一个三位数,它的每个位上的数字的3次幂之和等于它本身。(例如:153=13+53+35153=13+53+35)找到所有的水仙花数并按行打印,按从小到大的顺序输出。思路:首先确定范围,三位数(100--999),其次确定百位、十位、个位要怎么表示,令一个位数为i百位(a)a=i/100  ......
  • C语言解决切面条问题
    题目叙述:一根高筋拉面,中间切一刀,可以得到2根面条。如果先对折1次,中间切一刀,可以得到3根面条。如果连续对折2次,中间切一刀,可以得到5根面条。那么,连续对折10次,中间切一刀,会得到多少面条呢?分析:可以拿一张纸撕1条,对折3次从中间撕开是9块1次------3---2次------5---3次----......
  • C语言实现反转整数
    题目描述:从标准输入流(控制台)中获取一个整数 num,将其 按位反转 后通过输出语句输出反转后的整数,保留原来整数的正负性。思路:前提是num不等于0首先我们需要定义一个中间变量 temp 来存放当前 num 的最小位,获取最小位存在temp---temp=num%10通过 result=result*1......
  • c语言的特点
    C语言的特点可谓既鲜明又多样,下面列举几个主要的方面:简洁紧凑、灵活方便:C语言只有32个关键字,9种控制语句,程序书写形式自由,区分大小写。把高级语言的基本结构和语句与低级语言的实用性结合起来。C语言可以像汇编语言一样对位、字节和地址进行操作,而这一切又是在高级语言编译系......
  • 冒泡排序和选择排序--C语言
    冒泡排序(升序):设计思想:每两个相邻的数进行比较,大的往后走详细过程:例:99,100,88,80,100,90,77,22,33,90第一遍:99与100比较,100大,继续向后走,100与88比较,100大,100与88交换一下位置,继续向后走100与80比较,100大,100与80交换一下位置,继续向后走100与100比较,相等,不需要......
  • C语言中的printf和sprintf的用法及区别
    sprintf函数是C语言中用于格式化输出到字符串的函数。它的原型如下:intsprintf(char*buffer,constchar*format,[argument]…);str:指向存储输出结果的字符数组的指针。format:格式化字符串,包含要输出的文本和格式说明符。[argument]:可变参数列表,用于提供要插入格式化......
  • C语言常用格式字符
    %d或%i  有符号十进制整数%u  无符号十进制整数%ld  有符号长整型%c  字符%s  字符的字符串%f  十进制浮点数//------------------------分隔符------------------------%o  有符号八进制%x  无符号十六进制整数//--------------------......
  • 磁盘-输入输出-总线
    磁盘磁盘分为磁道和扇区磁盘的存取时间=寻道时间+等待时间(寻道时间耗时比等待时间长)寻道时间是磁头寻找到磁道的时间,等待时间就是等待读写的扇区转到磁头的时间寻道有以下调度算法先来先服务FCFS:就是按请求先来先服务最短寻道时间优先SSTF:先去离当前磁头最近的磁道(有......
  • 【C语言】模拟实现 atoi
    文章目录atoi()函数模拟实现思路分析代码呈现atoi()函数通过上述cplusplus和MSDN对atoi函数的介绍我们可以得出以下几个关键点库函数:<stdlib.h>形参:constchar*str返回值:int作用:atoi函数是将一个字符串转化成一个整型并忽视字符串中的字符举个例子/*atoi......