字符(串)输入输出函数

  • 输出函数

#include <stdio.h>

int fputc(int c, FILE *stream);

int fputs(const char *s, FILE *stream);

int putc(int c, FILE *stream);

int putchar(int c);

int puts(const char *s);

注:fputs() writes the string s to stream, without its terminating null byte ('\0').可以连续向流写字符串而一次读出。

puts() writes the string s and a trailing newline to stdout.返回值个数包含‘\n’。

RETURN VALUE

   fputc(),  putc()  and  putchar()  return  the  character written as an unsigned char cast to an int or EOF on error.
   puts() and fputs() return a nonnegative number(个数) on success, or  EOF  on error.
  • 输入函数

#include <stdio.h>

int fgetc(FILE *stream);

char *fgets(char *s, int size, FILE *stream);

int getc(FILE *stream);

int getchar(void);

char *gets(char *s);

int ungetc(int c, FILE *stream);

gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

fgets()在缓冲区最后加字符串结束符‘\0’,不越界,比gets()更安全。gets()禁用。两者均是读入一行字符。

返回值:

   fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.
   gets() and fgets() return s on success, and NULL on error or when  end of file occurs while no characters have been read.

注:如果一次fgets调用在读入若干个字符后到达文件末尾,则将以读入的字符串加上'\0'存入缓冲区并返回,如果再次调用fgets则返回NULL,可以据此推断是否读到文件未尾。

注:对于fgets来数'\n'是一个特殊字符,而'0'无特殊之处,如果读到'\0'就到普通字符读入。如果文件中存在'\0',调用fgets之后就无法判断缓冲区中'\0'究竟是从文件读上来的还是fgets自动添加的结束符。所以fgets只适合读文本文件而不适合读二进制文件,并且文本文件中所有的字符都应该是可见字符,不能是'\0'.

注:fgets空间小时‘\0’占用最后一个字符,其余舍弃;空间大时若有'\n',读入‘\n’。

gets空间小时有时可正常输出(不检查越界),异常情况自动加‘\0’;空间大时有'\n’,不读入‘\n’,不换行。

  • scanf

int scanf(const char *format, ...);

int fscanf(FILE *stream, const char *format, ...);

int sscanf(const char *str, const char *format, ...);

   The scanf() function reads input from the standard input stream stdin, fscanf()  reads  input  from  the  stream pointer stream, and sscanf() reads its input from the character string pointed to by str.
   The  format string consists of a sequence of directives which describe how to process the sequence of input characters.  If processing  of  a directive  fails,  no  further  input is read, and scanf() returns.  A "failure" can be either of the following: input failure, meaning  that input  characters  were unavailable, or matching failure, meaning that the input was inappropriate.

返回值:

   These  functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or  even  zero  in the event of an early matching failure.
   The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.  EOF  is also  returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is  set  indicate the error.

scanf()遇到white-sapce字符即结束,white-space: space(空格), form-feed(\f), newline(\n), carriage return(\r), horizontal tab(\t), and vertical tab(\v).

注:scanf只接收输入,不能输出。

  • printf

int printf(const char *format, ...);

int fprintf(FILE *stream, const char *format, ...);

int sprintf(char *str, const char *format, ...);

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

The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.

Return value

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.

If an output error is encountered, a negative value is returned.

注:EOF = -1

字符(串)输入输出函数相关推荐

  1. Linux怎么输出函数,Linux C 程序 输入输出函数(THREE)

    标准输入输出函数 #include stdio 是 standard input & output 的缩写 字符数据输入输出函数: putchar() , getchar() 格式化输入输出函 ...

  2. c语言如何持续输入字符直到指定字符结束_《小白学C》第三章 常用输入输出函数...

    第三章 常用输入/输出函数 与其他高级语言一样, C语言的语句是用来向计算机系统发出操作指令的. 当我们提到输入时,这意味着要向程序填充一些数据.输入可以是以文件的形式或从命令行中进行.C 语言提供了 ...

  3. c语言中向计算机输入一个字符的函数是,计算机c语言输入输出函数格式详解.docx...

    1.输入和输出: 输入:输入也叫读,数据由内核流向用户程序 输出:输出也称写.打印,数据由用户程序流向内核 以下介绍一些输入输出函数,尽管都是一些有缺陷的函数,但比较适合初学者使用 2.printf用 ...

  4. C语言中的输入输出函数

    ----------------------------- //键盘缓存区(按enter送入)==>输入缓存区 ==>scanf ----------------------------- ...

  5. c语言中调整颜色的函数_C语言中的输入输出函数

    点击上方"学士科技",选择"设为星标" 技术干货第一时间送达! 01 字符数据输入输出 字符数据输出函数putchar() C语言中字符数据输出使用的是putc ...

  6. 计算机输入输出c语言,计算机等级考试二级C语言讲义第三讲输入输出函数

    <计算机等级考试二级C语言讲义第三讲输入输出函数>由会员分享,可在线阅读,更多相关<计算机等级考试二级C语言讲义第三讲输入输出函数(5页珍藏版)>请在人人文库网上搜索. 1.第 ...

  7. scanf 与 printf 输入输出函数

    格式化输入输出函数 所谓格式化输入输出,就是不再如同上一节介绍的函数那样,都是单纯地输入输出一个字符或者一行文字.格式化输入输出,就是要将各种类型数据(包括整型.浮点型以及字符串等类型的常量或变量), ...

  8. C语言(输入输出函数getchar,putchar、gets、puts,scanf,printf的功能以及用法)

    常用的输入输出函数 getchar gets scanf putchar puts printf getchar int getchar( void ); 返回值为int,所以需要用一个int变量来接 ...

  9. 简单讲解c语言中各个输入输出函数使用场景与不同,填补你的知识盲区

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 C语言中有多个输入输出函数,各个输入输出函数可能因为运算方式的不同导致其形成差异,令他们的使用场景不同,我们大多数人可能只会使用他,却 ...

最新文章

  1. Git私服搭建与使用
  2. .py与.pyc文件区别
  3. python解压文件_Python压缩和解压缩文件(zip/unzip)详解
  4. ITK:二维高斯混合模型期望最大化
  5. PHP连接达梦数据库
  6. java 进度条jsp,jsp进度条_jsp技巧
  7. MSCI发布最新ESG评级 三七互娱位居A股游戏行业最高
  8. 用户变量和系统变量的区别_环境变量的用户变量与系统变量的区别
  9. 安卓三维展示源码_谷歌也翻车了?全球数亿安卓设备难逃一“劫”,用户隐私数据库被利用长达10年!...
  10. Com原理及應用——Com對象和接口
  11. 谈朋友圈——周围的朋友们
  12. CUMCM 2021-B:乙醇偶合制备C4烯烃(多元线性回归分析)
  13. HBase Coprocessor实现HBase二级索引
  14. meanshift算法图解
  15. Imagination发布开源项目:适配PowerVR IP的Vulkan驱动和编译器合入Mesa 3D 图形库
  16. pgadmin mac卸载_Mac软件卸载——安全彻底地在Mac上卸载Microsoft Outlook - Mac迪迪卫...
  17. ubuntu qq 以及 词典
  18. CSS3动画那么强,requestAnimationFrame还有毛线用--摘抄
  19. 弹性地基梁板法计算原理_地下连续墙静力计算和分析.pdf
  20. 用计算机套路电话号码,套路计算器

热门文章

  1. linux关机_3.5 开关机命令及7个运行级别《LINUX-centos7-操作系统入门到精通》
  2. java+读取source资源_如何从JavaJAR文件中读取资源文件?
  3. rust 死后不知道家在哪_赌王儿子何猷君被嘲妈宝,求婚不知道戒指戴哪只手,大喊求助妈妈...
  4. 超级计算机手机芯片,美国开建arm超级计算机,单节点性能是手机芯片100倍
  5. java 正则表达式 Pattern
  6. Spring 解耦工厂模式
  7. 3.5 定向搜索的误差分析
  8. c hello world
  9. 在sqlplus中实现命令的上翻下翻功能
  10. ESXi与Linux主机配置syslog日志上传远程服务器