#include
#include
int k,m,sum;
long long int i;
char a[1000];
int main()
{
printf("输入一张CD可存放歌曲数量");
scanf("%d",&k);
printf("输入一共多少首歌");
scanf("%d",&m);
if(k>m||k==m) printf("需要1张CD");
else if(m%k==0) printf("需要%d张CD",m/k);
else if(m>k&&m%k!=0) printf("你需要%d张CD\n",(int)(m/k+1));
/*********************************************/
for(i=8;i<=888888;i=(i*10)+8)
sum+=i;
printf("8+88+888+8888+88888+888888=%d\n",sum);
//这个出题人是有多无聊
/*********************************************/
fflush(stdin);
sum=0;
gets(a);
for(i=1;i
百度百科定义:
功能:清空输入缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符,此时应该先执行fflush(stdin);
注意:此函数仅适用于部分编译器(如VC6),但是并非所有编译器都要支持这个功能(如gcc3.2)。这是一个对C标准的扩充。
/*stdin就是标准输入 std即standard(标准),in即input(输入),合起来就是标准输入。 一般就是指键盘输入到 缓冲区里的东西。 */
函数名: fflush
功能: 清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
原型:int fflush(FILE *stream)
程序例
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void fflush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* create a file */
stream = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush\
DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without\
closing it */
flush(stream);
printf("\nFile was flushed, Press any key\
to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush the stream's internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush\
the DOS buffer */
close(duphandle);
}
返回值:
如果成功刷新,fflush返回0。指定的流没有 缓冲区或者只读打开时也返回0值。返回EOF指出一个错误。
注意:如果fflush返回EOF,数据可能由于写错误已经丢失。当设置一个重要错误处理器时,最安全的是用setvbuf函数关闭缓冲或者使用低级I/0例程,如open、close和write来代替流I/O函数。
C99 对 fflush 函数的定义:
编辑 int fflush( FILE* stream);
如果 stream指向 输出流或者 更新流(update stream),并且这个更新流
最近执行的操作不是输入,那么fflush函数将把任何未被写入的数据写入 stream
指向的文件(如标准输出文件 stdout)。否则,fflush函数的行为是 不确定的。
fflush( NULL)清空所有输出流和上面提到的更新流。如果发生 写错误,fflush
函数会给那些流打上错误标记,并且返回EOF,否则返回0。
简单来说就是在使用scanf和getchar前最好清空缓存区 do
{
scanf("%s",a);
printf("Continue?\n");
m=getch();
}while(m!='n'&&m!='N');
如果用户在提示语Continue后输入字符过多将会跳过scanf直接读取缓冲区。因为scanf会先读取缓冲区的内容。所以应当在scanf前加:
fflush(stdin);
不过首先需要知道的是,fflush(stdin);是由微软自己扩展的清空语句,实际上并不符合规范