读书人

ungetc的胜势在哪里

发布时间: 2013-07-08 14:13:00 作者: rapoo

ungetc的优势在哪里
下面是两个小程序是我练习ungetc的,但是一直也没有清楚它到底是干什么,书上对它也有解释,但就是看不明白。请大家帮我明白。它有什么不可替代的作用。

第一个小程序我根据书中输入whale songs,输出的是h.我怎么觉得应该是w.
第二个输出是w.


#include "stdafx.h"
#include "stdio.h"
#include "string.h"

int main(void)
{
char ch;

scanf("%c", &ch);
ch = getchar();
putchar(ch);

return 0;
}

#include "stdafx.h"
#include "stdio.h"
#include "string.h"

int main(void)
{
char ch;

scanf("%c", &ch);
ungetc(ch,stdin);
putchar(ch);

return 0;
}






[解决办法]
别想太多了
库函数他提供的功能就在摆在哪里
喜欢怎么灵活应用是自己的本事

[解决办法]
scanf("%c", &ch); //input 'while' ch='w'
ch = getchar(); //ch='h'
putchar(ch); //ch='h'

scanf("%c", &ch); //input 'while' ch='w'
ungetc(ch,stdin); //ch='w', not change, but the stdin should be 'while'

putchar(ch); //ch='w'

引用:
下面是两个小程序是我练习ungetc的,但是一直也没有清楚它到底是干什么,书上对它也有解释,但就是看不明白。请大家帮我明白。它有什么不可替代的作用。

第一个小程序我根据书中输入whale songs,输出的是h.我怎么觉得应该是w.
第二个输出是w.


#include "stdafx.h"
#include "stdio.h"
#include "string.h"

int main(void)
{
char ch;

scanf("%c", &ch);
ch = getchar();
putchar(ch);

return 0;
}

#include "stdafx.h"
#include "stdio.h"
#include "string.h"

int main(void)
{
char ch;

scanf("%c", &ch);
ungetc(ch,stdin);
putchar(ch);

return 0;
}

[解决办法]
int ungetc(int c, FILE *stream)

ungetc pushes c (converted to an unsigned char) back onto stream, where it will be returned on the next read. Only one character of pushback per stream is guaranteed. EOF may not be pushed back. ungetc returns the character pushed back, or EOF for error.

读书人网 >C语言

热点推荐