0

If I input abc as the text stream, getchar() reads it and putchar prints it. I used printf statements to trace whats exactly happening. But I get a strange output after I print the characters of input text stream. It enters once more the while loop and execute those two printf statements i.e when i is 4. Does this get my Enter key as an input. If "yes" I also tried using fflush but that creates yet another issue. No character gets printed after I fflush creating problems with getchar. Why is this happening and how to fix it?

What happens if I use fflush(stdin)

#include <stdio.h>
main(){
   c=getchar();
   fflush(stdin);
   while(c!= EOF)
   {
     putchar(c);
     c=getchar();
    fflush(stdin);
   }
}

I write ABC on console when using fflush. It just getchar() 'a' and prints it. It doesn't even get the remaining characters.

Below it is the main program that I was talking about before I used fflush.

#include <stdio.h>

main()
{
    int c,i=1;

    c=getchar();

    while(c!= EOF)  // while(c=getchar!=EOF) putchar(c);
    {
      printf("\n\nthis is upper %d time in loop i.e. before the putchar\n\n",i);
      putchar(c);
      printf("\n\nthis is down %d time in loop i.e. after the putchar\n\n",i);
      c=getchar();
      i++;
    }
}
pablo1977
  • 4,281
  • 1
  • 15
  • 41
  • 1
    [`fflush(stdin)` is undefined behaviour](http://stackoverflow.com/questions/2979209/using-fflushstdin?lq=1); anything can happen. – legends2k Sep 09 '14 at 00:55
  • 2
    _I write ABC on console using fflush. It just getchar() 'a' and prints it. It doesn't even get the remaining characters._ Because left off the input buffer by `fflush(stdin)`. – BLUEPIXY Sep 09 '14 at 01:09
  • @BLUEPIXY thanks i got it but what about the fix to this program, isn't there any way to not read in the \n character and i that assures guarantee unlike fflush – Stack Overflow 32 Sep 09 '14 at 01:37
  • even the my country's best university prof. use fflush. It's not my fault. That is the reason i prefer stackoverflow come here to learn from you guys and coz that guarantee success unlike fflush which doesn't guarantee anything – Stack Overflow 32 Sep 09 '14 at 01:41
  • you can check `c=='\n'`. What would you like to do? E.g. Display by removing newline from the input. – BLUEPIXY Sep 09 '14 at 01:43
  • yes i want to remove this '\n' so it doesn't enters while loop and execute the printf statements 4th time...if i give the input text stream of size 3 – Stack Overflow 32 Sep 09 '14 at 01:45
  • 1
    @StackOverflow32 why don't you break out of the loop when `c == '\n'` – M.M Sep 09 '14 at 02:00
  • "... and how to fix it?" How to fix *what*??? What is your *intended* behavior? Nowhere in your question you explain how you want your program to work. The 4 iterations are perfectly expected: yes, it reads "abc" characters and the key, as it should. So, what do you want to "fix" and why? – AnT stands with Russia Sep 09 '14 at 02:57

2 Answers2

1
#include <stdio.h>

int main(){
    int c;

    while((c=getchar())!=EOF && c != '\n'){
        putchar(c);
    }
    return 0;
}

while((c=getchar())!=EOF){
    if(c == '\n')
        break;//exit this while-loop
    putchar(c);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • if i use c == '\n' instead of c!= '\n' than what happened it just terminates. In `abc` when i press enter i.e. '\n' atleast it should print `abc` and than terminate but it just terminates after i type `abc` and press enter....WHY? – Stack Overflow 32 Sep 09 '14 at 02:27
  • 1
    @StackOverflow32 Loop is terminated condition becomes false when `c=='\n'` is not satisfied. – BLUEPIXY Sep 09 '14 at 02:30
0

The loop is executed 4 times because pressing enter causes a newline to show up on input. So, when i == 4, you will have c == '\n'.

You shouldn't fflush(stdin), that's undefined behavior. Don't do it.

And also, this won't work the way you want:

while(c=getchar!=EOF) putchar(c);

Comparison has higher precedence than assignment, which means that this block of code is the same as:

while (c = (getchar() != EOF)) putchar(c);

Which is wrong. You have to use parentheses to give the assignment a higher priority:

while ((c = getchar()) != EOF) putchar(c);
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • yaa that was "while(c=getchar!=EOF) putchar(c);" just not the part of the code....i know that != has higher precedence than = so c will get 1 or 0....i don't put brackets around (c=getchar()) – Stack Overflow 32 Sep 09 '14 at 01:28
  • In all of this `c` should be `int c;` so that it can store EOF properly. – M.M Sep 09 '14 at 01:28
  • but what is the answer to the question how can i fix it and not get enter or \n character read by getchar()? @FilipeGonçalves – Stack Overflow 32 Sep 09 '14 at 01:30
  • @StackOverflow32 Just put it in the loop condition: `while ((c = getchar()) != EOF && c != '\n') { ... }` – Filipe Gonçalves Sep 09 '14 at 03:18