Skip to content

Instantly share code, notes, and snippets.

@gnitnaw
Created June 10, 2015 13:30
Show Gist options
  • Save gnitnaw/ac3dbcd8fa8e11c515c8 to your computer and use it in GitHub Desktop.
Save gnitnaw/ac3dbcd8fa8e11c515c8 to your computer and use it in GitHub Desktop.
fflush use?
#include <stdio.h>
#define MAXSIZE 256
void read_string(char* p);
int main(void) {
char c, s[MAXSIZE];
puts("I/O lib");
puts("");
printf("Please give me a char: ");
c = getchar();
printf("What you keyin is %c\n", c);
// fflush(stdin);
while(getchar()!='\n');
printf("Please give me a string : ");
read_string(s);
printf("What you keyin is %s\n", s);
printf("\n Press <Enter> to continue...");
while ((c=getchar()) != '\n');
return 0;
}
void read_string(char* p) {
int i;
char c;
for (i=0; i<MAXSIZE-1; ++i) {
if ( (c=getchar()) != '\n' ) {
p[i] = c;
} else {
break;
}
}
p[i] = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment