ungetc()这个函数怎么用法??

snowingedge 2001-05-12 01:03:00
期待....
...全文
146 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
personnel 2001-05-14
  • 打赏
  • 举报
回复
/* UNGETC.C: This program first converts a character
* representation of an unsigned integer to an integer. If
* the program encounters a character that is not a digit,
* the program uses ungetc to replace it in the stream.
*/

#include <stdio.h>
#include <ctype.h>

void main( void )
{
int ch;
int result = 0;

printf( "Enter an integer: " );

/* Read in and convert number: */
while( ((ch = getchar()) != EOF) && isdigit( ch ) )
result = result * 10 + ch - '0'; /* Use digit. */
if( ch != EOF )
ungetc( ch, stdin ); /* Put nondigit back. */
printf( "Number = %d\nNextcharacter in stream = '%c'",
result, getchar() );
}


Output

Enter an integer: 521a
Number = 521
Nextcharacter in stream = 'a'
l_little 2001-05-14
  • 打赏
  • 举报
回复
看联机帮助塞

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧